C + + provides constructors and destructors for initialization and cleanup of data members.
constructor function:
Defined:
1. C + + defines a special member function that is the same as the class name, that is, the constructor.
2, with or without reference.
3, no return type.
Call:
Automatically called, the C + + compiler will automatically call the constructor;
Call manually: In some cases you need to call manually.
After defining two objects T1 and T2, the constructor calls T2 first, the destructor is called in reverse order, and T2 is called after T1 is called first.
No parameter constructor:
After the object is defined outside the class, the parameterless constructor is called automatically, and the object data member is assigned internally;
There are parametric constructors:
Bracket method: Text T1 (3,4);
Equal sign method: Text T2 = (1,2,3,4); A comma-expression returns the rightmost number. A parameter
Manual call: Text T3 = T1 (3,4); Initialization of an object, assignment constructor
Text T4; T4 = T3; The assignment of an object is called by the C + + operator = () or user-overloaded equal-sign action function.
The initialization of an object is a different concept from the assignment of an object.
copy Constructor : Initializes another object with 1 objects:
1, Text T2 = T1;
2, Text T2 (T1);
3, call F (T2), p call copy constructor, with T2 to complete its own initialization. At this point, p is an element that is equivalent to T2 (non-pointer, non-reference)
void f (Text p) { << p.fun () <<
4. The return value of the function is an element that returns an anonymous object , so the copy constructor of the anonymous object class is called
void display () { g (); // after a destructor, the anonymous object is not used, and the anonymous object is refactored.
Text m = g (); An anonymous object Initializes an object of the same type, and the anonymous object is turned into a known object
Text N (UP);
n = g (); An anonymous object is assigned to another object of the same type, and the anonymous object is refactored.
}text g () { Text A (1,2); // call a parameter constructor return A; // call the copy constructor to create an anonymous object. }
Constructor Call rules Study:
When defining a class, there is no write constructor, and the C + + compiler provides a default constructor.
You must use it to write a constructor.
Shallow copy problem: (Write copy constructor yourself: mainly for pointer members)
1. Copy constructor function
Text obj2 = obj1; Call Obj2 copy constructor Text (text& zty) {}
If there are two variables in text, one of them is a pointer.
Char* p;
int Len;
If the copy constructor is not defined, the copy constructor for C + + is called, at this time:
The OBJ1 member variable p and Len are all in the stack area, and the P pointer holds the address information, pointing to the data in the heap (the global variable area is copied, note: The global variable area and the heap area have this part of the data)
C + + comes with a copy constructor that performs a shallow copy, just copies P and Len to obj2 in the stack, so p also points to the data area in the heap. At the end of the life cycle of the two variables, the Obj2 normal destruction, the data destruction, and the p in the pointer complex null,obj1 become the wild pointer when the destruction is needed.
2, equal sign operator
Text obj1 ("SF");
Text obj2 ("SAFDSG"); Calling a parameter constructor in a class
Obj2 = obj1; If you do not reload operator= () yourself, call C + + 's own = operator for a simple copy of the value of P and Len, the area of the heap that the pointer refers to is not copied, and the pointer p in both OBJ1 and Obj2 point to the data in the same area of the heap.
Initialization list of constructors:
Resolves a class-a object in Class B (where Class A constructors are designed)
The constructor of a is designed according to the calling rules of the constructor, must be used, there is no chance of initializing a in B
The new syntax:
classa{ Public: A (int_a) {a=_a; } ~A () {}Private: intA;}classb{ Public: B::b (int_B1): A2 (2), A3 (3) {B1=_b1; } b::b (int_B1,intV2,intv3): A2 (V2), A3 (v3) {B1=_b1;}Private: intB1; A A2; A A3;}voidMain () {B zty (1); B Lunais (1,2,3);}
the constructor A of the grouped object is executed first, in the order of definition, not in the order in which the list is initialized. Destructors are reversed.
If there is a const object in the class, the initial value must be assigned with the initialization list
classb{ Public: B::b (int_B1): A2 (2), A3 (3), C (20) {B1=_b1; } b::b (int_B1,intV2,intV3): A2 (V2), A3 (V3), C (20)//c can also be assigned to other values {B1=_b1;}Private: Const intC; intB1; A A2; A A3;}
constructor _ deep copy and shallow copy of destructor function _