Anonymous objects: Temporary objects are usually released when they are constructed (special case, return value optimization)
1. Return value optimization: If the anonymous object returned by the function returns with a new object of the same type, the anonymous object is converted to a new object.
#include"iostream"using namespacestd;classa{ Public: A (int_a=0,int_b=0) { This->A1 =_a; This->B1 =_b; cout<<"construct function called! "<<Endl; } A (a&obj) {cout<<"copy_constructor function called!"<<Endl; } ~A () {cout<<"objext destory function called!"<<Endl; }protected:Private: intA1; intB1;};//function return value produces anonymous objecta G () {a a2 (Ten, +); returnA2;}//Test One: An anonymous object is used to initialize a new object. voidTest1 () {A A1=g ();}//Test Two: Initialize a new object with an equal signvoidTest2 () {A A3; A3=g ();}intMain () {Test1 (); Test2 (); return 0;}
Test1 Result: Two constructors were called, two destructors
construct function called! Constructs A2
copy_constructor function called! Anonymous object that constructs anonymous A2
objext destory function called! destructor local variable A2
objext destory function called! destructor A1 (in fact, A2 anonymous object)
************************** ********************************/
Test2 Result: Three constructor three-time destructor was called
Construct function called! Construction A3
Construct function called! Construction A2
Copy_constructor function called! Anonymous object that constructs anonymous A2
Objext destory function called! local variable A2
Objext destory function called! destructor A2 anonymous object
**********************************************************/
2. No object name: normal situation, after the construction is finished, is directly destroyed
#include"iostream"using namespacestd;classa{ Public: A (int_a=0,int_b=0) { This->A1 =_a; This->B1 =_b; cout<<"construct function called! "<<Endl; } A (a&obj) {cout<<"copy_constructor function called!"<<Endl; } ~A () {cout<<"objext destory function called!"<<Endl; } voidprintf () {cout<< This->A1 <<" "<< This->B1 <<Endl; }protected:Private: intA1; intB1;};intMain () {A (Ten,Ten). printf (); cout<<"before printing here, the anonymous object has been refactored! "<<Endl; return 0;}
Points to note:
1. All formal parameters provide a constructor for the default argument, and a default constructor is also defined, and such a constructor parameter list is a physical parameter (the formal parameter of a parameter constructor has an initial value, which is equivalent to writing the default constructor)
2. Whether an anonymous object is being refactored to see if the return value has an object to connect to
3. Three scenarios for copying constructors:
<1> A1 = A2 (distinguishes between two different cases: A AA; A BB = AA; will//bb = AA; Not): When an object initializes another object
<2> func (A A1): When an object is a function parameter
<3> A A2 = func (): When a function returns a value of an object (an issue involving an anonymous object)
4. Special Note: The equal sign operation and the initialization of the object are two different concepts
C + + Temporary anonymous object