A little in-depth understanding of the C + + replication control process through an instance, reference to C + + primer, introduces the basics:
1. In C + +, classes pass special member functions: copy constructors, assignment operators, and destructors to control what happens when you copy, assign, and revoke objects of that class.
2. copy Constructor (copy constructor) is a special constructor with a single formal parameter, which is a reference to the class type;
- When a new object is defined and initialized with an object of the same type, the use of the copy constructor is displayed;
- When you pass an object of that class to a function or return an object of that type from a function, the copy constructor is used implicitly.
3. The copy constructor can be used to:
- Displays or implicitly initializes an object based on another object of the same type;
- Copy an object and pass it as an argument to a function;
- Copy an object from the function return;
- Initializes the elements in the sequential container;
- Initializes an array element based on an element initializer list.
4. If we do not define a copy constructor in the class body, the compiler will synthesize a copy constructor for us. Unlike the composition's default constructor, a copy constructor is synthesized even if we define other constructors.
composition copy Constructor behavior: Performs a member-by-group initialization to initialize a new object to a copy of the original object.
Some classes often have data members that are pointers, and we have to control what happens when the object is copied, otherwise when the copied object is refactored or deleted, the pointer in the object no longer points to a valid object, then a dangling pointer condition occurs, and we have to define our own copy constructor.
5, some classes need to completely prohibit replication, for example, the Iostream class does not allow replication, if you do not want to copy, it should be explicitly prohibited, you can declare the copy constructor is private, and is not defined.
6, assignment operator (assignment operator): As with the copy constructor, if there is no assignment operator defined in the class body, the compiler will also synthesize an assignment operator for us. It performs a member- by-copy: Each member of the right operand object is assigned to the corresponding member of the left operand object.
Such as:
sales_item& sales_item::operator= (const Sales_item &rhs) { ISBN = RHS.ISBN; Units_sold = Rhs.units_sold; Revenue = Rhs.revenue; return *this;}
7, destructor (destructor): is the complement of the constructor, when the object is out of scope or dynamically allocated objects (new, malloc) is deleted, the destructor is automatically applied.
The following is a detailed description of the copy constructor, assignment operator, destructor call mechanism, the code has been explained in detail, so don't repeat it here ~
1/************************************************ 2 * 3 * Content Summary: Defines the EXPM1 class, which gives copy control members and other constructors 4 * Use ex in different ways Object of type PM1:5 * As a non-reference parameter and a reference parameter pass, the dynamic allocation of 6 * as a function return value, the assignment operation, as the element is placed in the vector container. 7 * Study when to execute which constructor and copy control member 8 *
9 * Date: 2012.8.27 by Jacky Liu10 *11 ************************************************/12 #include <vector> ; #include <iostream>15-struct Expm117 {18//default constructor Expm1 () {std::cout<< "Expm1 () "<<std::endl;22}23 24//copy constructor EXPM1 (const EXPM1 &) std::cout<< ( Const expm1&) "<<std::endl;28}29 30//Copy operator = Expm1 & operator = (const EXPM1 &) 32 {33 std::cout<< "operator = (const expm1&)" <<std::endl;34 return *this;35}36 37//destructor 3 8 ~expm1 () std::cout<< "~expm1 ()" <<std::endl;41}42};43 $ void func1 (EXPM1 obj) The formal parameter is EXPM1 object {}46-Func2 (EXPM1 & obj)//parameter is a reference to the Expm1 object {}49 Expm1 func3 ()} Expm1 obj;53 return obj; Returns EXPM1 Object}55 () std::cout<< "1-------------------------------------" <<std::endl< <STD:: endl;59 Expm1 eobj; Call the default constructor to create a Expm1 object eobj61 std::cout<< "2-------------------------------------" <<std::endl<< std::endl;63 func1 (eobj); Call the Copy constructor 65//Create a copy of the parameter Expm1 object as an argument EXPM1 object 66//After the function finishes executing, call the destructor to undo the parameter Expm1 object. ::cout<< "3-------------------------------------" <<std::endl<<std::endl;69 Func2 (eobj); The parameter is a reference to the EXPM1 object without calling the copy constructor to pass the argument. std::cout<< "4-------------------------------------" <<std::endl <<std::endl;73 eobj = func3 (); Call the default constructor to create a local EXPM1 object 75//The function returns when the copy constructor is called to create the Expm1 object as a copy of the return value 76//Then call the destructor to undo the local Expm1 Object 77//Then call assignment operator 78//After performing the assignment operation 79//Call the destructor to revoke the EXPM1 pair as a copy of the return value Like the Bayi std::cout<< "5-------------------------------------" <<std::endl<<std::endl;82 Expm1 *p = new Expm1; Calling the default constructor to dynamically create EXPM1 objects Std::vectoR<expm1> Evec (3); Call the default constructor to create a temporary value EXPM1 object 86//Call the copy constructor to copy the temporary value EXPM1 object to each element of the vector container Evec 87 Call destructor to revoke a temporary value EXPM1 object 88//Repeat three times by above std::cout<< "6------ -------------------------------"<<std::endl<<std::endl;91 Delete p; Call destructor to undo dynamically created Expm1 object The system ("pause"); 94 return 0; Eobj and Evec end of life, automatically call destructor Undo 95 96}
Run Result: carefully observe the execution of the running results, you can understand the replication control mechanism in C + + ~
A little deeper understanding C + + replication Control "Go"