Chapter 4 semantic learning during execution
6.0 IntroductionDefine and operate the following classes: class y {public: Y ();~ Y (); bool operator = (const Y &) const ;}; Class X {public: X ();~ X (); operator y () const; // conversion X getvalue () ;}; then the following expression: If (yy = xx. getvalue () is converted to {x temp1 = XX during execution. getvalue (); // introduce the temporary variable Y temp2 = temp1.operator y (); // introduce the Temporary Variable int temp3 = yy of the Y type. operator = (temp2); // introduce the temporary variable of int type if (temp3 )...... Temp2.y ::~ Y (); // y destructor temp1.x ::~ X (); // X destructor}
6.1 Object Construction and AnalysisNote: If a partition or function has multiple exit points, the Destructor must be placed before each exit point (if the object is valid at the time ).
6.1.1 Global ObjectFor the following program snippet: matrix identity; int main () {matrix M1 = identity; return 0;} c ++ guarantees that before identity is used for the first time in the main function, construct the identity, and destroy the identity before the main function ends. If such global objects have constructors and destructor, static initialization and memory release operations are required. All global objects in the C ++ program are placed in the Data Segment of the program. If you explicitly specify a value for it, the object uses this value as the initial value, otherwise, the memory content configured for the object is 0 (the initial value is not automatically set in C ).
6.1.2 local static objectThe local static objects in the function can be constructed only once and analyzed once, and are constructed upon execution of the definition point. In cfront, make sure that this action is correctly completed by introducing a temporary amount to indicate whether the data segment has been constructed. In the analysis, the address in the data segment is retrieved to call it properly.
6.1.3 object ArrayFor array definition: Point knot [10]; If point does not define constructor or destructor, it will not work more than creating an array of built-in types, and only need to configure enough memory to store 10 consecutive point elements. However, if constructors and destructor are defined, they must take turns on each element. In cfront, A vec_new () function is used to generate an array constructed from class objects. The vec_new () Statement is generally as follows: void * vec_new (void * array, size_t elem_size, int elem_count, void (* constructor) (void *), void (* destructor) (void *, char); similarly, the corresponding vec_delete function is used for destruction, its declaration is as follows: void * vec_delete (void * array, size_t elem_size, int elem_count, void (* destructor) (void *, char ));
6.1.4 default constructor and arrayIt doesn't make much sense, so ignore the internal implementation.
6.2 new and delete Operators. For details, see "C ++ dynamic memory creation and memory management learning notes ".
6.3 temporary objectsReal temporary objects in C ++ are invisible, and they do not appear in your source code. Creating an unnamed non-heap object will generate a temporary object. This unnamed object is usually generated under two conditions: implicit type conversion and function return objects for successful function calls. Whenever you see the reference-to-const parameter, there is a possibility of creating a temporary object and binding it to the parameter. A temporary object will be created (released later) whenever you see the returned object of the function ). There are the following principles for the survival of temporary objects: the destruction of temporary objects should be the last step in the process of creating a complete expression for the temporary object. But there are two exceptions: 1. When an expression is used to initialize an object, it is required to survive until the initialization of the object is complete;
2. when a temporary object is bound to a reference object, the object will be destroyed only when the referenced declaration ends or the declaration scope of the temporary object ends (depending on which situation the object arrives first.