|
|
 |
 |
 |
 |
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
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 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
> > 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 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
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.
|
 |
 |
 |
 |
|