compiler synthesized constructors/copy constructors/assignment operators

Discussion in 'C++' started by Jess, Jun 6, 2007.

  1. Jess

    Jess Guest

    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
    Jess, Jun 6, 2007
    #1
    1. Advertising

  2. Jess

    Devon Null Guest

    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.
    Devon Null, Jun 6, 2007
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Jun 6, 2007
    #3
  4. Jess

    Craig Scott Guest

    > > 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
    Craig Scott, Jun 6, 2007
    #4
  5. 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
    Victor Bazarov, Jun 6, 2007
    #5
  6. Jess

    Ron Natalie Guest

    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.
    Ron Natalie, Jun 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Ford
    Replies:
    1
    Views:
    5,262
    Ron Natalie
    Nov 20, 2003
  2. Replies:
    1
    Views:
    508
    Victor Bazarov
    Feb 7, 2005
  3. John Ratliff
    Replies:
    2
    Views:
    336
    John Ratliff
    Aug 10, 2005
  4. Jeremy Smith
    Replies:
    2
    Views:
    570
    Jeremy Smith
    Aug 3, 2006
  5. srp113
    Replies:
    3
    Views:
    453
Loading...

Share This Page