6th. The implementation period semantics (Runtime semantics) Imagine the following simple equation:
if (yy = = Xx.getvalue ()) //...
where xx and yy are defined as:
X xx; Y yy;
The definition of Class Y is:
Class Y {public: y (); ~y (); BOOL operator== (const Y &) const;};
Class X is defined as:
Class X {public: x (); ~x (); operator Y () const; Conversion operator X getValue ();};
first determine the real entity that the equality operator refers toIn this example, it will be resolved (resolved) as the "Y member entity being overloaded".
here is the first conversion of the equation:
Resolution of intended Operatorif (yy.operator== (Xx.getvalue ())) //...
the equality operator for y requires a parameter of type Y, whereas GetValue () returns a true object of type X, if there is no way to convert an X object to a Y object, then this formula is wrong. In this case
X provides a conversion that converts an X object to a Y object. It must be performed on the return value of GetValue ().
here is the second conversion of the equation:
Conversion of GetValue () ' s return Valueif (yy.operator== (Xx.getvalue (). operator Y ())) //...
Everything that has happened so far is a "fat" operation done by the compiler on the program code, based on the implied semantics of class. If necessary, you can also explicitly write such a formula.
Although the semantics of the program are correct, its education is not yet correct. A temporary object must then be generated to place the value returned by the function call:
produces a temporary class X object that places the return value of GetValue ():
X Temp1 = Xx.getvalue ();
produces a temporary class Y object that places the return value of operator Y ():
Y Temp2 = Temp1.operator y ();
produces a temporary int object that places the return value of the equality operator:
int Temp3 = yy.operator== (TEMP2);
Finally, the appropriate destructor will be applied to each temporary class object, which causes the equation to be converted to the following form:
C + + Pseudo code: The following is the conditional clause if (yy = = Xx.getvalue ()) ... The conversion { X Temp1 = Xx.getvalue (); Y Temp2 = Temp1.operator y (); int Temp3 = yy.operator== (TEMP2); if (Temp3) //... Temp2. Y::~y (); Temp1. X::~x ();}
It seems like a lot, it's a difficult thing for C + +: It's not easy to see the complexity of the expression from the program code. This chapter looks at some of the transformations that take place during the execution period.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + object model-Deconstruction Semantics (fifth chapter)