/ | ||||
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++20) | ||||
(C++20) |
(C++11) | ||||
expression |
pointer |
specifier | ||||
specifier (C++11) | ||||
specifier (C++11) |
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++11) |
General | ||||
/ types | ||||
types | ||||
Members | ||||
pointer | ||||
-declarations | ||||
(C++11) | ||||
specifier | ||||
specifier | ||||
Special member functions | ||||
(C++11) | ||||
(C++11) | ||||
Inheritance | ||||
specifier (C++11) | ||||
specifier (C++11) |
A copy constructor is a constructor which can be called with an argument of the same class type and copies the content of the argument without mutating the argument.
Syntax Explanation Implicitly-declared copy constructor Implicitly-defined copy constructor Deleted copy constructor Trivial copy constructor Eligible copy constructor Notes Example Defect reports See also |
class-name parameter-list | (1) | ||||||||
class-name parameter-list function-body | (2) | ||||||||
class-name single-parameter-list | (3) | (since C++11) | |||||||
class-name parameter-list | (4) | (since C++11) | |||||||
class-name class-name parameter-list function-body | (5) | ||||||||
class-name class-name single-parameter-list | (6) | (since C++11) | |||||||
class-name | - | the class whose copy constructor is being declared |
parameter-list | - | a non-empty satisfying all following conditions: , the first parameter is of type T&, const T&, volatile T& or const volatile T&, and . |
single-parameter-list | - | a of only one parameter, which is of type T&, const T&, volatile T& or const volatile T& and does not have a default argument |
function-body | - | the of the copy constructor |
The copy constructor is called whenever an object is initialized (by direct-initialization or copy-initialization ) from another object of the same type (unless overload resolution selects a better match or the call is elided ), which includes
If no user-defined copy constructors are provided for a class type, the compiler will always declare a copy constructor as a non- explicit inline public member of its class. This implicitly-declared copy constructor has the form T :: T ( const T & ) if all of the following are true:
Otherwise, the implicitly-declared copy constructor is T :: T ( T & ) .
Due to these rules, the implicitly-declared copy constructor cannot bind to a volatile lvalue argument.
A class can have multiple copy constructors, e.g. both T :: T ( const T & ) and T :: T ( T & ) .
Even if some user-defined copy constructors are present, the user may still force the implicit copy constructor declaration with the keyword default. | (since C++11) |
The implicitly-declared (or defaulted on its first declaration) copy constructor has an exception specification as described in dynamic exception specification (until C++17) noexcept specification (since C++17) .
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 odr-used or needed for constant evaluation (since C++11) . For union types, the implicitly-defined copy constructor copies the object representation (as by std::memmove ). For non-union class types, the constructor performs full member-wise copy of the object's direct base subobjects and member subobjects, in their initialization order, using direct initialization. For each non-static data member of a reference type, the copy constructor binds the reference to the same object or function to which the source reference is bound.
If this satisfies the requirements of a (until C++23) (since C++23), the generated copy constructor is constexpr. The generation of the implicitly-defined copy constructor is deprecated if has a user-defined destructor or user-defined copy assignment operator. | (since C++11) |
The implicitly-declared or explicitly-defaulted (since C++11) copy constructor for class T is undefined (until C++11) defined as deleted (since C++11) if any of the following conditions is satisfied:
has a non-static data member of rvalue reference type. | (since C++11) |
The implicitly-declared copy constructor for class is defined as deleted if declares a or . | (since C++11) |
The copy constructor for class T is trivial if all of the following are true:
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.
TriviallyCopyable objects can be copied by copying their object representations manually, e.g. with std::memmove . All data types compatible with the C language (POD types) are trivially copyable.
A copy constructor is eligible if it is either user-declared or both implicitly-declared and definable. | (until C++11) |
A copy constructor is eligible if it is not deleted. | (since C++11) (until C++20) |
A copy constructor is eligible if all following conditions are satisfied: (if any) are satisfied. than any other copy constructor. | (since C++20) |
Triviality of eligible copy constructors determines whether the class is an implicit-lifetime type , and whether the class is a trivially copyable type .
In many situations, copy constructors are optimized out even if they would produce observable side-effects, see copy elision .
[ edit ] defect reports.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
C++98 | the conditions where implicitly-declared copy constructors are undefined did not consider multi-dimensional array types | consider these types | |
C++11 | volatile members make copy non-trivial ( ) | triviality not affected | |
C++11 | X(X&) = default was non-trivial | made trivial | |
C++20 | a copy constructor was not eligible if there is another copy constructor which is more constrained but does not satisfy its associated constraints | it can be eligible in this case |
IMAGES
VIDEO
COMMENTS
If a new object has to be created before the copying can occur, the copy constructor is used. If a new object does not have to be created before the copying can occur, the assignment operator is used. Example for assignment operator: Base obj1(5); //calls Base class constructor.
Copy constructor and Assignment operator are similar as they are both used to initialize one object using another object. But, there are some basic differences between them: Copy constructor. Assignment operator. It is called when a new object is created from an existing object, as a copy of the existing object.
The assignment operator is to deal with an already existing object. The assignment operator is used to change an existing instance to have the same values as the rvalue, which means that the instance has to be destroyed and re-initialized if it has internal dynamic memory. Useful link : Copy Constructors, Assignment Operators, and More
Use an assignment operator operator= that returns a reference to the class type and takes one parameter that's passed by const reference—for example ClassName& operator=(const ClassName& x);. Use the copy constructor.
Copy assignment operator. A copy assignment operator is a non-template non-static member function with the name operator= that can be called with an argument of the same class type and copies the content of the argument without mutating the argument.
The main difference between Copy Constructor and Assignment Operator is that the Copy constructor makes a new memory storage every time it is called while the assignment operator does not make new memory storage.
What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance. According to the C++ standard, the copy constructor for MyClass must have one of the following signatures:
The Copy constructor and the assignment operators are used to initializing one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space.
A copy constructor is a constructor that is used to initialize an object with an existing object of the same type. After the copy constructor executes, the newly created object should be a copy of the object passed in as the initializer. An implicit copy constructor.
A copy constructor is a constructor which can be called with an argument of the same class type and copies the content of the argument without mutating the argument. Syntax. class-name. - the class whose copy constructor is being declared. parameter-list. - a non-empty parameter list satisfying all following conditions: