- # Include <iostream>
- Using namespace STD;
- Class B
- {
- Public:
- B ()
- {
- Cout <"default constructor" <Endl;
- }
- B (B & B)
- {
- Cout <"copy constructor" <Endl;
- }
- ~ B ()
- {
- Cout <"destructed" <Endl;
- }
- B (int I): Data (I)
- {
- Cout <"Constructed by parameter" <data <Endl;
- }
- B & operator = (B & B)
- {
- Cout <"========" <Endl;
- Return * this;
- }
- PRIVATE:
- Int data;
- };
- B play (B)
- {
- Return B;
- }
- Int main ()
- {
- B (5 );
- // Unknown object, constructed by B (INT) first, and then analyzed
- B T1 = play (B (4 ));
- // Call the constructor B (INT) of B when loading play (5), and construct instance B of B according to parameter 5
- // When return B returns, call B's copy constructor to construct a new temporary object and return
- // The Return Statement is out of B's scope. At this time, the destructor of B is called.
- B t2 = play (T1 );
- // Call the copy constructor of B when loading play (T1) to construct object B
- // When return B, call the copy constructor of B to construct a new temporary object.
- // Then call the destructor of B
- T2 = T1;
- // Partial objects t1 and t2 exit the scope and call their destructor
- Return 0;
- }
Output:
Constructed by parameter 5
Destructed
Constructed by parameter 4
Copy constructor
Destructed
Copy constructor
Copy constructor
Destructed
==========
Destructed
Destructed
- A named Automatic object is created every time the program executes its declaration and destroyed every time the program leaves the block (scope) It appears;
- A free storage object is created using the new operator and destroyed using the delete operator;
- A non-static member object, as a member of another class object, is also created or destroyed when it is created or destroyed as the member object;
- An array object is created or destroyed when it is created or destroyed as the array of elements;
- A local static object is created when it is declared for the first time during program execution, and destroyed once upon program termination;
- A global object, namespace object, and static object of the class. They are created only once at the beginning of the program and destroyed once at the end of the program;
- A temporary object is created as part of the expression evaluation and destroyed at the end of the complete expression it appears;
- An object that is controlled by the provided parameters in the allocation operation and stored in the storage obtained through the functions provided by the user;
- A union object, which cannot have constructor and destructor.
(The above part is taken from <C ++ programming language>, p218)
About how to calculate sizeof (class)
Null: 1
No virtual function: Sum of sizeof (data member)
There are virtual functions: sizeof (data member) and + sizeof (V Table pointer) = 4
Multiple threads in the same process share code segments (code and constants), data segments (static and global variables), and extended segments (Heap Storage ),However, each thread has its own stack segment. Stack segments are also called runtime stacks, which are used to store all local variables and temporary variables (parameters, return values, temporary constructed variables, etc ).