Default functions generated by the C ++ Compiler
Topic introduction:
How does one assign and copy objects? What are their differences? Can an empty custom type perform these operations?
Object Value assignment:
"Operator overload
User A (10), B;
B =;
Object replication: Call the copy constructor.
User B;
User A (B );
Or
User A = B; // equivalent to user A (B );
It also calls the copy constructor.
Differences between the two:A value assignment is to assign values to an existing object (an object has been defined to be assigned), while copying creates a new object from scratch, and make it the same as an existing object.
Shallow replication and deep replication:If a pointer member exists in the object, only the address of the pointer member is copied to the newly created object. Therefore, the pointer members of the two objects point to the same memory area, the issue of repeated release may occur during release. You need to manually define the copy constructor. In the constructor, a new memory is allocated for the pointer variable, which indicates that pointer members of different objects direct to different memory regions.
There are three situations where the copy constructor is used:
1. Create a new object and initialize it with another similar object.
2. When function parameters are class objects, you must create a copy of the real parameters when calling the function, and copy a form parameter according to the real parameters. The system implements this by calling the copy constructor.
3. the return value of a function is the class object. When a function call ends, you need to copy a temporary object from the function and pass it to the function call.
User getuser () {user temp; return temp;} int main () {user = getuser (); // call getuser ();}
When the call to the getuser () function ends, the life cycle of the object temp created in getuser is terminated (to be destroyed). Therefore, when the return statement is executed, call the copy constructor of the user class, copy a new object according to temp, and assign it to the user.
Default functions provided by the compiler:
The above mentioned object copy construction and value assignment operations. If you want to perform these operations for a custom type, do you need additional function definitions? The answer is: no special requirements are required. The compiler has provided us with the default implementation.Note that these default implementations are bit-by-bit replication methods.
Defines an empty C ++ class, such
class Empty{}
An empty class is no longer blank after being processed by the C ++ compiler. The Compiler automatically declares some member functions for us.
Class empty {public: Empty (); // default constructor empty (const empty &); // copy constructor ~ Empty (); // destructor empty & operator = (const empty &); // value assignment operator empty * operator &(); // address retrieval operator const empty * operator & () const; // address retrieval operator const };
The following is an example of a complete type definition for implementing these functions:
Class student {public: Student (); // constructor ~ Student (); // destructor student (const student & Stu); // copy constructor student & operator = (const student & Stu ); // value assignment operator bool operator = (const student & Stu) const; // = Operator overload public: Char name [10]; int age ;}; // constructor Student: Student () {trace ("Student constructor \ r \ n");} // destructor Student ::~ Student () {trace ("Student's destructor \ r \ n");} // copy the constructor Student: Student (const student & Stu) {trace ("Student's replication constructor \ r \ n"); sprintf (name, Stu. name); age = Stu. age ;}// value assignment operator student & Student: Operator = (const student & Stu) {trace ("Student's assignment operator \ r \ n "); // The member variables of this object should be released to point to the original memory (this type is not involved )//... sprintf (name, Stu. name); age = Stu. age; return * This;} bool Student: Operator = (const student & Stu) const {trace ("= Operator \ r \ n"); Return strcmp (name, stu. name) = 0 & age = Stu. age ;}