compiler synthesized constructors/copy constructors/assignment operators - c++

This is a discussion on compiler synthesized constructors/copy constructors/assignment operators - c++ ; Hi, I understand that if I define a constructor, then the compiler won't synthesize any constructor for me. How about copy constructor and assignment operator? WIll compiler stop synthesizing any of them if I declare a constructor or something else? ...

+ Reply to Thread
Results 1 to 6 of 6

compiler synthesized constructors/copy constructors/assignment operators

  1. Default compiler synthesized constructors/copy constructors/assignment operators

    Hi,

    I understand that if I define a constructor, then the compiler won't
    synthesize any constructor for me. How about copy constructor and
    assignment operator? WIll compiler stop synthesizing any of them if I
    declare a constructor or something else?

    Thanks,
    Jess


  2. Default Re: compiler synthesized constructors/copy constructors/assignmentoperators

    Jess wrote:
    > Hi,
    >
    > I understand that if I define a constructor, then the compiler won't
    > synthesize any constructor for me. How about copy constructor and
    > assignment operator? WIll compiler stop synthesizing any of them if I
    > declare a constructor or something else?
    >
    > Thanks,
    > Jess
    >


    No - it will create defaults for any one you do not define yourself.

    --
    [there are no x's in my email]

    I have the right to remain silent
    (and should probably use it as much as possible)
    Anything I type can and will be used against me
    in a court of idiocy
    I have the right to be wrong
    (and probably am)
    If I can not furnish my own wrongness
    I'm sure someone will provide it for me.

  3. Default Re: compiler synthesized constructors/copy constructors/assignment operators

    Devon Null wrote:
    > Jess wrote:
    >> Hi,
    >>
    >> I understand that if I define a constructor, then the compiler won't
    >> synthesize any constructor for me. How about copy constructor and
    >> assignment operator? WIll compiler stop synthesizing any of them if
    >> I declare a constructor or something else?
    >>
    >> Thanks,
    >> Jess
    >>

    >
    > No - it will create defaults for any one you do not define yourself.


    Depends on whether it can be done. For example, copy assignment op
    is impossible to generate if there's a data member who's a reference,
    IIRC.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask



  4. Default Re: compiler synthesized constructors/copy constructors/assignment operators

    > > I understand that if I define a constructor, then the compiler won't
    > > synthesize any constructor for me. How about copy constructor and
    > > assignment operator? WIll compiler stop synthesizing any of them if I
    > > declare a constructor or something else?

    >
    > No - it will create defaults for any one you do not define yourself.


    It's a bit more complicated than that. If you write any of your own
    constructors, the compiler will not generate a default constructor for
    you (see 12.1/5). Also, if you have a template constructor taking a
    single parameter, I believe that the compiler will still generate an
    implicit copy constructor even if the template version would otherwise
    be able to take the same parameter as the copy constructor. Anyone
    able to provide evidence for or against this in the standard? The
    closest I can find is 14.5.2/2, but that is talking about which one is
    called when a template and non-template member function are both
    present with the same name, not constructors specifically.

    --
    Computational Fluid Dynamics, CSIRO (CMIS)
    Melbourne, Australia


  5. Default Re: compiler synthesized constructors/copy constructors/assignment operators

    Craig Scott wrote:
    > [..] if you have a template constructor taking a
    > single parameter, I believe that the compiler will still generate an
    > implicit copy constructor even if the template version would otherwise
    > be able to take the same parameter as the copy constructor. Anyone
    > able to provide evidence for or against this in the standard? The
    > closest I can find is 14.5.2/2, but that is talking about which one is
    > called when a template and non-template member function are both
    > present with the same name, not constructors specifically.


    12.8/3, the second sentence.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask



  6. Default Re: compiler synthesized constructors/copy constructors/assignmentoperators

    Jess wrote:
    > Hi,
    >
    > I understand that if I define a constructor, then the compiler won't
    > synthesize any constructor for me. How about copy constructor and
    > assignment operator? WIll compiler stop synthesizing any of them if I
    > declare a constructor or something else?
    >
    >

    No, if you define ANY constructor, the implicit default constructor is
    omitted. If you don't define a copy constructor, a copy constructor
    is generated. The latter is true for the copy-assignment operator.


+ Reply to Thread

Similar Threads

  1. Copy constructors
    By Application Development in forum c++
    Replies: 10
    Last Post: 10-19-2007, 03:03 AM
  2. Copy Constructors vs Move Constructors
    By Application Development in forum c++
    Replies: 3
    Last Post: 08-04-2007, 01:34 PM
  3. Replies: 5
    Last Post: 08-03-2007, 07:11 AM
  4. Replies: 4
    Last Post: 03-19-2007, 06:04 PM
  5. Anonymous union rules against constructors & assignment
    By Application Development in forum c++
    Replies: 72
    Last Post: 06-13-2006, 09:38 PM