- When it comes to a class type object, the replication form and the direct form of the initialization are different: direct initialization calls directly to the constructor that matches the argument, and replication initialization always calls the copy constructor. Copy initialization first creates a temporary object with the specified constructor, and then copies that temporary object to the object being created with the copy constructor.
- For vector<string> Svec (5); The compiler first uses the string default constructor to create a temporary value to initialize the Svec, and then uses the copy constructor to copy the temporary values to each element of the Svec
- Some classes require a complete ban on replication. For example, the Iostream class does not allow replication, and if you want to suppress replication, it seems possible to omit the copy constructor, however, if you do not define a copy constructor, the compiler will synthesize one. Therefore, in order to prevent replication, the class must display its copy constructor declared as private. If the copy constructor is private, user code will not be allowed to replicate objects of that class type, and the compiler will reject any attempts to replicate. However, friends and members of the class can still replicate. If you want to disallow replication in friends and members, you can declare a private copy constructor but not define it. Declaring a member function without defining it is legal, but any attempt to use an undefined member will cause the link to fail. By declaring (but not defining) the private copy constructor, you can suppress any attempt to replicate a class type object: The copy attempt in user code is marked as error at compile time, and the copy attempt in the member function and friend causes an error at link time.
- A class can define its own assignment operator, which, in general, requires an assignment operator if the class requires a copy constructor. In fact, these two operators should be considered as a unit. If one is needed, we almost certainly need another.
- Dynamically allocated objects revoke a resource only if the pointer to that object is deleted. If you do not delete a pointer to a dynamic object, the destructor for the object is not run, the object persists, causing a memory leak, and any resources that are used inside the object are not freed. When a container is revoked (whether it is a standard or built-in array), the destructor for the element of the class type in the container is also run, and the elements in the container are always reversed in reverse order, first undoing the element labeled Size ()-1, then ... The element until the subscript is 0.
- If a class requires a destructor, it also requires an assignment operator and a copy constructor, which is a useful rule of thumb.
- Unlike a copy constructor or assignment operator, the compiler always synthesizes a destructor for us, and the composition destructor revokes each non-static member in reverse order when the object was created, so that it revokes the member in the reverse of the sequence in which the member is declared in the class, for each member of the class type, The composition destructor calls the member's destructor to undo the object.
- Although you can define more than one constructor for a class, you cannot overload a destructor, you can only provide a destructor that applies to all objects of the class. An important difference between a destructor and a copy constructor or assignment operator is that, even if we write our own destructor, the destructor of the composition is still running. The user-defined destructor is first performed, and then the composition destructor is performed.
- The copy operator first checks whether the object is the same, assigns a value to delete the left operand, and after the left operand's member is undone, assigns the member of the right operand to the corresponding member of the left operand, or, if the object is the same, revokes the member of the right operand by revoking the member of the left operand. Even if the object is assigned to itself, the correct work of the assignment operator is also important, and the common way to ensure this behavior is to display the assignment of checking itself.
- Classes that allocate memory or other resources almost always need to define replication control members to manage allocated resources, and if a class requires destructors, it almost always needs to define copy constructors and assignment operators.
- An assignment operator can be overloaded to define the meaning of assigning a class type object to another type object. The member of the assignment operator class and must return a reference to the owning class object. If the class does not have an assignment operator defined, the compiler will synthesize one.
C + + replication control