打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

2016.03.09

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

#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
}


看输出:

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C++拷贝构造函数
C++类对象的拷贝构造函数
为什么不要在构造函数中调用虚函数
类内部存储的东西:太简洁了----小话c++(5)
boost::optional<>
《c++ primer》 第12章 动态内存 学习笔记
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服