C + + function returns an example of a class object

Source: Internet
Author: User
Tags visual studio

The following example uses the compiler in Visual Studio 2012 (the Default and O2 optimization results are not the same) and g++4.7.2 (MinGW) results.

The code is as follows Copy Code


#include <iostream>
using namespace Std;
Class A
{
Public
int A;

A (int i): A (i)
{
cout << "A ():" << a << Endl;
}

~a ()
{
cout << "~a ()" << A << Endl;
}

A (const a& x): A (X.A)
{
cout << "Copy A ()" << a << Endl;
}
};
A test1 ()
{
return A (1);
}
A test2 ()
{
A tmp (2);
return TMP;
}
int main ()
{
A A = Test1 ();
A B = test2 ();
cout << "Before exit" << Endl;
return 0;
}

As you can see, the compiler has done the relevant optimizations in the face of this situation, and the VS2012 compiler can see the copy constructor call by default.

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.