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.