手机
当前位置:查字典教程网 >编程开发 >C语言 >C++函数返回值为对象时,构造析构函数的执行细节
C++函数返回值为对象时,构造析构函数的执行细节
摘要:看如下代码:复制代码代码如下:#includeclassTestConstructor{public:TestConstructor(){s...

看如下代码:

复制代码 代码如下:

#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、调用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

}

看输出:

C++函数返回值为对象时,构造析构函数的执行细节1

有注释,有输出。执行细节,一目了然了吧

【C++函数返回值为对象时,构造析构函数的执行细节】相关文章:

函数式宏定义与普通函数的区别

函数指针与指针函数的学习总结

函数外初始化与函数内初始化详细解析

C++中引用的使用总结

C++函数参数取默认值的深入详解

基于errno返回值的对应错误码的详细介绍

深入解析C++中的构造函数和析构函数

C++函数重载的深入解析

深入探讨:宏、内联函数与普通函数的区别

探讨:C++中函数返回引用的注意事项

精品推荐
分类导航