作者:奶牛还在Henry | 来源:互联网 | 2021-12-12 18:53
看如下代码:
代码如下:
#include
class TestConstructor
{
public:
TestConstructor()
{
std::cout<<"TestConstructor()"< }
~TestConstructor()
{
std::cout<<"~TestConstructor()"< }
TestConstructor(const TestConstructor& testObj)
{
std::cout<<"TestConstructor(const TestConstructor&)"< }
TestConstructor& operator = (const TestConstructor& testObj)
{
std::cout<<"TestConstructor& operator = (const TestConstructor& testObj)"< return *this;
}
};
TestConstructor testFunc()
{
TestConstructor testInFunc; //3、调用TestConstructor() 生成对象testInFunc
return testInFunc; //4、调用TestConstructor(const TestConstructor&) 生成临时对象
//5、调用析构函数,析构对象testInFunc
}
int main()
{
TestConstructor test; //1、调用TestConstructor() 生成对象test
test = testFunc(); //2、调用testFunc() //6、调用等号把临时对象复制给对象test //7、调用析构函数,析构临时对象
return 0; //8、调用析构函数,析构对象test
}
看输出:
![](https://img.php1.cn/3cccf/2481c/a6e/c6cd6bab6722d59a.jpeg)
有注释,有输出。执行细节,一目了然了吧