C++函數(shù)返回值為對象時,構(gòu)造析構(gòu)函數(shù)的執(zhí)行細節(jié)
更新時間:2013年02月18日 11:44:57 作者:
C++函數(shù)返回值為對象時,構(gòu)造析構(gòu)函數(shù)的執(zhí)行細節(jié),需要的朋友,可以參考下
看如下代碼:
復制代碼 代碼如下:
#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、調(diào)用TestConstructor() 生成對象testInFunc
return testInFunc; //4、調(diào)用TestConstructor(const TestConstructor&) 生成臨時對象
//5、調(diào)用析構(gòu)函數(shù),析構(gòu)對象testInFunc
}
int main()
{
TestConstructor test; //1、調(diào)用TestConstructor() 生成對象test
test = testFunc(); //2、調(diào)用testFunc() //6、調(diào)用等號把臨時對象復制給對象test //7、調(diào)用析構(gòu)函數(shù),析構(gòu)臨時對象
return 0; //8、調(diào)用析構(gòu)函數(shù),析構(gòu)對象test
}
看輸出:
有注釋,有輸出。執(zhí)行細節(jié),一目了然了吧
相關文章
C++實現(xiàn)轉(zhuǎn)置矩陣的循環(huán)
大家好,本篇文章主要講的是C++實現(xiàn)轉(zhuǎn)置矩陣的循環(huán),感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01C++中constexpr與函數(shù)參數(shù)轉(zhuǎn)發(fā)的操作方法
constexpr是c++11引入的關鍵字,c++11的constexpr的函數(shù)中只是支持單句代碼,c++14限制放寬,可以在里邊寫循環(huán)及邏輯判斷等語句,本文探討關于constexpr的函數(shù)中參數(shù)的現(xiàn)象,以及如果參數(shù)是constexpr如何做轉(zhuǎn)發(fā),感興趣的朋友一起看看吧2024-02-02C++ STL priority_queue自定義排序?qū)崿F(xiàn)方法詳解
這篇文章主要介紹了C++ STL priority_queue自定義排序?qū)崿F(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03