Copy constructor is a special constructor with a single parameter. This parameter (commonly used for const modification) is a reference to this type. When you define a new object and initialize it with an object of the same type, the copy constructor is explicitly used. When you pass an object of this type to a function or return an object of this type from a function, the copy constructor is implicitly used.
A destructor is a complementary constructor. When an object that exceeds the scope or is dynamically assigned is deleted, destructor applies the destructor to itself. Destructor can be used to release resources that are constructed when an object is released or obtained during its life cycle. No matter whether the class defines its own destructor, the compiler automatically executes the destructor of non-static data members in the class.
The value assignment operator (assignment operator) can be overloaded by specifying different types of right operands. The version with the right operand as the class type is special: if we do not write this version, the compiler will synthesize it for us.
The copy constructor, value assignment operator, and destructor are called copy control ). The compiler automatically implements these operations, but the class can also define its own version.
There is a special common case where the class defines its own replication control members: the class has pointer members.
13.1 copy constructor
There is only one form parameter, and this form parameter is a reference to this type of object (commonly used const modifier). Such a constructor is called a copy constructor. Like the default constructor, the copy constructor can be implicitly called by the compiler. The copy constructor can be used:
Initialize an object explicitly or implicitly based on an object of the same type.
Copy an object and pass it as a real parameter to a function.
Copy an object when the function returns.
Initialize elements in the sequential container.
Initializes array elements based on the element initialization list.
1. object definition form
C ++ supports two initialization modes: Direct initialization and replication initialization. The = symbol is used for replication initialization, and the initialization formula is placed in parentheses for direct initialization.
When an object of the class type is used, the initialization replication mode is different from the direct mode: the constructor that matches the real parameter is called directly during initialization, And the replication constructor is always called during replication initialization. Copy initialization first creates a temporary object using the specified constructor, and then copies the temporary object to the object being created using the copy constructor.
StaticClass cs1 ();
StaticClass cs2 = StaticClass ();
For a class object, you must specify a single real parameter or explicitly create a temporary object for replication.
Because I/O type objects cannot be copied, copying initialization cannot be used for those types of objects.
2. Participate in the return value
When the parameter is not of the reference type, the value of the real parameter is copied. Similarly, when the return value is a non-reference type, a copy of the value in the return statement is returned.
When the form parameter or return value is of the class type, the copy constructor replicates the data.
3. initialize container elements
The copy constructor can be used to initialize elements in an ordered container. For example, you can use a single parameter that represents the capacity to initialize the container. This constructor of the container uses the default constructor and the copy constructor.
Vector <string> vec (5 );
As a general rule, unless you want to use the default initial values of container elements, a more effective way is to allocate an empty container and add the values of known elements to the container.
4. constructor and array elements
If no element Initialization is provided for an array of class types, each element is initialized using the default constructor. However, if you use the array initialization list enclosed by regular curly braces to provide an explicit element initialization type, you can use copy initialization to initialize each element. Create an element of the appropriate type based on the specified value, and then use the copy constructor to copy the value to the corresponding element.
Class Class5
{
Public:
Int k;
Class5 (int I): k (I ){}
};
Class5 arr [4] = {1, 2, 3, Class5 (4 )};
From xufei96's column