C ++: Is the default copy constructor the presence of other constructors and destructors?

As we know , if any constructor is declared (including the copy constructor), a default constructor (one that takes no arguments) is implicitly created. Does the same thing happen with the default copy constructor (the one that executes the shallow copy of the object)? Also, does the presence of a destructor affect this?

+8
c ++ copy-constructor default-constructor
source share
5 answers

The answer here is correct, but not complete. They are true for C ++ 98 and C ++ 03. In C ++ 11, you won’t get a copy constructor if you declared a move constructor or move an assignment operator. Also, if you declared a copy assignment operator or destructor, the implicit generation of the copy constructor is deprecated. 12.8 [class.copy]:

If the class definition does not explicitly declare the copy constructor, there is no user-declared constructor move, and there is no operator-declared assignment of the operator move, the copy constructor is implicitly declared as default (8.4.2). Such an implicit expression is deprecated if the class has a custom copy assignment operator or a user-declared destructor.

+5
source share

12.8 # 4 Copying class objects

If the class definition does not explicitly declare the copy constructor, one is declared implicitly

And the destructor doesn't matter

+9
source share

Not. You will get a default copy constructor if you do not provide your own copy constructor, and the presence or absence of a destructor does not matter.

+3
source share

Not. note that

MyClass { template <typename T> MyClass(const T&); }; 

does not provide a copy constructor and is generated by default.

+3
source share

A default copy constructor is always created if you do not define your own. A constructor without arguments is not defined with any other constructor in order to avoid calling it and, therefore, skip the code of the real constructor.

+1
source share

All Articles