国产探花免费观看_亚洲丰满少妇自慰呻吟_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
}

看輸出:

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

 

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大石桥市| 玉树县| 贺州市| 凯里市| 沙雅县| 涡阳县| 霍林郭勒市| 古交市| 南雄市| 巍山| 濮阳市| 合水县| 文成县| 博爱县| 涞源县| 乌审旗| 游戏| 祁连县| 革吉县| 江津市| 青浦区| 黄骅市| 沙坪坝区| 罗江县| 通州区| 柘城县| 紫云| 巢湖市| 孝感市| 房山区| 吴堡县| 温宿县| 平凉市| 泸定县| 万盛区| 五台县| 遂平县| 常德市| 子洲县| 甘孜| 巴马|