C ++: RVO (Return Value optimization)

Source: Internet
Author: User
# Include <stdio. h>

Class RVO

{

Public:

RVO ()

{

Printf ("I am in constructor % d \ n", this );

}

RVO (const RVO & c_rvo)

{

Printf ("I am in copy constructor % d \ n", this );

}

~ RVO ()

{

Printf ("I am in destructor % d \ n", this );

}

RVO & operator = (const RVO & RHs)

{

Printf ("I am in operator % d = % d \ n", this, & RHs );

Return * this;

}

Int mem_var;

};

RVO mymethod (int I)

{

Printf ("3 \ n ");

RVO; // I am in constructor 1244896

Printf ("4 \ n ");

RVO. mem_var = I;

Printf ("5 \ n ");

Return (RVO); // 1244948cc ,~ 1244896

Printf ("6 \ n ");

}

Int main ()

{

Printf ("1 \ n ");

RVO; // I am in constructor 1245016

Printf ("2 \ n ");

RVO = mymethod (5); // I am in copy constructor

Printf ("7 \ n ");

Return 0 ;//~ 1245016

}

RVO mymethod (RVO & hddenobj, int I)

{

Printf ("3 \ n ");

RVO;
RVO. RVO (); // I am in constructor 1244896

Printf ("4 \ n ");

RVO. mem_var = I;

Printf ("5 \ n ");

Hddenobj. RVO (RVO); // I am in copy constructor
RVO .~ RVO ();

Return; // 1244948cc ,~ 1244896

Printf ("6 \ n ");

}

Int main ()

{

Printf ("1 \ n ");

RVO; // I am in constructor 1245016

Printf ("2 \ n ");

RVO = mymethod (5); // I am in copy constructor

Printf ("7 \ n ");

Return 0 ;//~ 1245016

}

1
I am in constructor 1245016
2
3
I am in constructor 1244888
4
5
I am in copy constructor 1244944
I am in destructor 1244888
I am in operator 1245016 = 1244944
I am in destructor 1244944
7
I am in destructor 1245016

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.