The main principles are as follows: 3 and 4
I. copy constructors and value assignment operators
Class;
A;
A B1 (a); // copy the constructor instance
A b2 = A; // an example of the value assignment operator:
Ii. Copy the application instance of the constructor
1. display or implicitly Initialize an object of the same type, as shown below:
Class;
A;
A B ();
2. As the real parameter of the function, as follows:
Void work (const A & );
3. As the return value of the Function
A & geta () const
{
...
Return;
}
4. initialize elements of the sequential container
5. initialize array elements based on the element initialization list
3. The copy constructor for merging and the assignment operator for merging are as follows:
If you do not explicitly define a copy constructor or a value assignment operator, the compiler will synthesize a copy constructor by default. The merging and copying constructor and the merging and assignment operators are different from the assignment operators. By default, the execution behavior of the function synthesized by the compiler is "initialize members one by one ", initialize the new object as a copy of the original object. One-by-one initialization does not include static members, but is only responsible for initializing non-static members.
4. as mentioned above, to prevent copying and assigning values, the class can display and declare that its copy constructor and value assignment operator are private. If the copy constructor is private, user code will not be allowed to copy objects of this type, and the compiler will reject any replication attempts. At this time, the friends and members of the class can still be copied. Of course, it is easy to disable this situation. The following code does not support copying classes:
class NonCopyable{ protected: NonCopyable(){} ~NonCopyable(){} private: NonCopyable(const NonCopyable &); const NonCopyable& operator=(const NonCopyable&);};