源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-11-24 11:12 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C++函数返回值为对象时,构造析构函数的执行细节
看如下代码:
[u]复制代码[/u] 代码如下:
#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 }
看输出: [img]http://files.jb51.net/file_images/article/201302/2013218113225639.jpg[/img] 有注释,有输出。执行细节,一目了然了吧    
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部