When the C + + function returns the value as an object, the execution details of the constructor are constructed _c language

Source: Internet
Author: User

Look at the following code:

Copy Code code as follows:

#include <iostream>
Class TestConstructor
{
Public
TestConstructor ()
{
std::cout<< "TestConstructor ()" <<std::endl;
}
~testconstructor ()
{
std::cout<< "~testconstructor ()" <<std::endl;
}
TestConstructor (const testconstructor& testobj)
{
std::cout<< "TestConstructor (const testconstructor&)" <<std::endl;
}
testconstructor& operator = (const testconstructor& testobj)
{
std::cout<< "testconstructor& operator = (const testconstructor& testobj)" <<std::endl;
return *this;
}
};
TestConstructor TestFunc ()
{
TestConstructor Testinfunc; 3. Invoke TestConstructor () to generate objects Testinfunc
return testinfunc; 4. Invoke TestConstructor (const testconstructor&) to generate temporary objects
5, call destructor, destructor object Testinfunc
}
int main ()
{
TestConstructor test; 1. Invoke TestConstructor () to generate object test
Test = TestFunc (); 2. Call TestFunc ()//6, call the equal sign to copy the temporary object to the object Test//7, invoke the destructor, and destructor the temporary object
return 0; 8. Call destructors, destructor test
}

Look at the output:

There are comments, there is output. Executive detail, at a glance.

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.