zoukankan      html  css  js  c++  java
  • 函数返回值为对象时,构造析构的执行细节

    看如下代码:

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

    看输出:

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

  • 相关阅读:
    21 viewPager--- hzScrollView ----llContainer
    21 ViewPager RadioGroup
    CLEAR REFRESH FEEE的区别
    在ALV中更新数据库表
    cl_gui_cfw=>flush
    cl_gui_cfw=>dispatch
    数据库表-DD01L DD02L DD03L-保存数据表和域的消息
    SAP 锁机制
    ABAP 搜索帮助
    SAP Basis常用事务代码
  • 原文地址:https://www.cnblogs.com/yemsheng/p/2915318.html
Copyright © 2011-2022 走看看