Construction and destruction of objects:
Global objects
All global objects in a C + + program are placed in the program's data segment. If you explicitly assign it a value, this object will be the initial value. Otherwise, object is configured to have a content of 0.
If a global object has a constructor or destructor, we say it requires a static initialization operation and a memory release operation. The compiler performs the following steps:
1. Generate a _sti () function for each file that needs to be statically initialized, including the necessary constructor call operations.
2. In each file that requires a static memory release operation, a _STD () function is generated that contains the necessary destructor operations.
3. Insert the _main () function in the main function (to invoke all the _sti () functions in the execution file), and an exit () function (to invoke all the _STD () functions).
Object array:
Suppose we have the following array definitions:
Point knots[];
If point does not define a constructor and does not define a destructor, then our work is no more than building an array of "built-in (built-in)" types, That means we just need to configure enough memory to store 10 contiguous point elements.
If point does define a default constructor, the Desturctor must be piloted on top of each element in turn. This is generally achieved through one or more runtime library functions.
If point also defines a destructor, the destructor must also be tested on those 10 point elements when knots life is over. 、
If the programmer provides one or more obvious initial values to an array of class objects, like this, how to:
Point knots[] = {point (); Point (1.01.00.5), -1.0};
The compiler does not need to initialize for those elements that have an obvious initial value. For those objects that are not initialized, the compiler still needs to call the library function to initialize. That is to say, the first 7 objects of this definition are initialized by the compiler-generated code.
New and delete operators:
The use of operator new seems to be a single operation, like this:
int New int (5);
But in fact it is done in two steps:
1. Configure the required memory with an appropriate instance of the new operator function:
// call the new operator in a library function int *pi = _new (sizeof(int));
2. Set the initial value of the configured object:
5;
New Semantics for arrays:
If we define an array:
New Point3d[array_size];
Delete []p_array] must be called at the time of deletion. Because only the brackets appear, the compiler looks for the dimensions of the array, otherwise it assumes that only a single objects is to be deleted. If the programmer does not provide the necessary brackets, like this:
Delete P_array;
Then the first element of freedom will be deconstructed. Other elements still exist-although their associated memory has been asked to be returned.
Do not point a base-class pointer to an array of inherited class objects.
"C + +" Deep Exploration of C + + object Model reading notes--implementation-time semantics (Runtime semantics)