代码
#include <iostream>
using namespace std;
static int g_int = 0;
class MyClass
{
public:
MyClass()
{
a = g_int++;
std::cout << "Default constructor: " << a << std::endl;
}
MyClass(const MyClass& c)
{
a = g_int++;
std::cout << "Copy constructor: " << a << std::endl;
}
~MyClass()
{
std::cout << "Default destructor: " << a << std::endl;
}
private:
int a{ 0 };
};
MyClass test()
{
cout << "Front of Constructor" << endl;
MyClass t;
cout << "Behind of Constructor" << endl;
cout << "Front of test()::return" << endl;
return t;
}
int main()
{
cout << "Front of test()" << endl;
MyClass t = test();
cout << "Behind of test()" << endl;
cout << "Front of main()::return" << endl;
return 0;
}
结果
Front of test()
Front of Constructor
Default constructor: 0
Behind of Constructor
Front of test()::return
Copy constructor: 1
Default destructor: 0
Behind of test()
Front of main()::return
Default destructor: 1