C++函數(shù)返回值為對象時,構(gòu)造析構(gòu)函數(shù)的執(zhí)行細(xì)節(jié)
更新時間:2013年02月18日 11:44:57 作者:
C++函數(shù)返回值為對象時,構(gòu)造析構(gòu)函數(shù)的執(zhí)行細(xì)節(jié),需要的朋友,可以參考下
看如下代碼:
復(fù)制代碼 代碼如下:
#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)用等號把臨時對象復(fù)制給對象test //7、調(diào)用析構(gòu)函數(shù),析構(gòu)臨時對象
return 0; //8、調(diào)用析構(gòu)函數(shù),析構(gòu)對象test
}
看輸出:

有注釋,有輸出。執(zhí)行細(xì)節(jié),一目了然了吧
相關(guān)文章
利用C語言模擬實(shí)現(xiàn)qsort,strcpy,strcat,strcmp函數(shù)
這篇文章主要為大家詳細(xì)介紹了如何通過C語言模擬實(shí)現(xiàn)qsort(采用冒泡的方式),strcpy,strcat,strcmp等函數(shù),文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-11-11
C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07

