cpp/language/copy constructor

A copy constructor of class is a non-template  whose first parameter is &zwj;, &zwj;, &zwj;, or &zwj;, and either there are no other parameters, or the rest of the parameters all have default values.

Syntax
Where must name the current class (or current instantiation of a class template), or, when declared at namespace scope or in a friend declaration, it must be a qualified class name.

Explanation
@1@ Typical declaration of a copy constructor. @2@ Forcing a copy constructor to be generated by the compiler. @3@ Avoiding implicit generation of the copy constructor.

The copy constructor is called whenever an object is (by  or ) from another object of the same type (unless  selects a better match or the call is ), which includes
 * initialization: or, where  is of type ;
 * function argument passing:, where is of type  and  is ;
 * function return: inside a function such as, where  is of type , which has no.

Implicitly-declared copy constructor
If no user-defined copy constructors are provided for a class type (,, or ), the compiler will always declare a copy constructor as a non- member of its class. This implicitly-declared copy constructor has the form if all of the following are true: Otherwise, the implicitly-declared copy constructor is. (Note that due to these rules, the implicitly-declared copy constructor cannot bind to a volatile lvalue argument.)
 * each direct and virtual base of  has a copy constructor whose parameters are  or ;
 * each non-static data member of  of class type or array of class type has a copy constructor whose parameters are  or.

A class can have multiple copy constructors, e.g. both and.

The implicitly-declared (or defaulted on its first declaration) copy constructor has an exception specification as described in.

Deleted implicitly-declared copy constructor

 * has non-static data members that cannot be copied (have deleted, inaccessible, or ambiguous copy constructors);
 * has direct or virtual base class that cannot be copied (has deleted, inaccessible, or ambiguous copy constructors);
 * has direct or virtual base class or a non-static data member with a deleted or inaccessible destructor;

Trivial copy constructor
The copy constructor for class is trivial if all of the following are true:
 * it is not user-provided (that is, it is implicitly-defined or defaulted);
 * has no virtual member functions;
 * has no virtual base classes;
 * the copy constructor selected for every direct base of is trivial;
 * the copy constructor selected for every non-static class type (or array of class type) member of is trivial;

A trivial copy constructor for a non-union class effectively copies every scalar subobject (including, recursively, subobject of subobjects and so forth) of the argument and performs no other action. However, padding bytes need not be copied, and even the object representations of the copied subobjects need not be the same as long as their values are identical.

objects can be copied by copying their object representations manually, e.g. with std. All data types compatible with the C language (POD types) are trivially copyable.

Eligible copy constructor
Triviality of eligible copy constructors determines whether the class is an, and whether the class is a trivially copyable type.

Implicitly-defined copy constructor
If the implicitly-declared copy constructor is not deleted, it is defined (that is, a function body is generated and compiled) by the compiler if. For union types, the implicitly-defined copy constructor copies the object representation (as by std). For non-union class types ( and ), the constructor performs full member-wise copy of the object's bases and non-static members, in their initialization order, using direct initialization.