C + + Temporary anonymous object

Source: Internet
Author: User

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-&GT;A1 =_a;  This-&GT;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-&GT;A1 =_a;  This-&GT;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-&GT;A1 <<" "<< This-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.