compiler synthesized constructors/copy constructors/assignment operators

J

Jess

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
 
D

Devon Null

Jess said:
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.
 
V

Victor Bazarov

Devon said:
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
 
C

Craig Scott

I understand that if I define a constructor, then the compiler won't
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.
 
V

Victor Bazarov

Craig said:
[..] 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
 
R

Ron Natalie

Jess said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top