国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++函數返回值為對象時,構造析構函數的執行細節

2020-01-26 16:22:13
字體:
來源:轉載
供稿:網友

看如下代碼:

復制代碼 代碼如下:

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

看輸出:

有注釋,有輸出。執行細節,一目了然了吧

 

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉木萨尔县| 塔城市| 永寿县| 汶川县| 宣武区| 兴文县| 海安县| 连南| 阳春市| 拜泉县| 商丘市| 当雄县| 秀山| 永昌县| 平利县| 谢通门县| 康保县| 台南县| 永善县| 项城市| 成安县| 瑞丽市| 塔河县| 当雄县| 吴堡县| 嘉义县| 昔阳县| 鹿邑县| 永修县| 济宁市| 济源市| 龙门县| 江油市| 彭山县| 奉贤区| 潼关县| 潼关县| 开远市| 苗栗市| 定远县| 荥阳市|