Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Is default copy constructor affected by presence of other constructors and destructor?

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

like image 223
Septagram Avatar asked Apr 11 '11 09:04

Septagram


2 Answers

12.8 #4 Copying class objects

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

And the destructor plays no part

like image 160
dubnde Avatar answered Sep 30 '22 00:09

dubnde


The answers here are correct but not complete. They are correct for C++98 and C++03. In C++11 you will not get a copy constructor if you have declared a move constructor or move assignment operator. Furthermore if you have declared a copy assignment operator or a destructor, the implicit generation of the copy constructor is deprecated. 12.8 [class.copy]:

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

like image 22
Howard Hinnant Avatar answered Sep 29 '22 23:09

Howard Hinnant