深入C++中構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、賦值操作符、析構(gòu)函數(shù)的調(diào)用過(guò)程總結(jié)
更新時(shí)間:2013年05月29日 10:16:48 作者:
本篇文章是對(duì)C++中構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、賦值操作符、析構(gòu)函數(shù)的調(diào)用過(guò)程進(jìn)行了總結(jié)與分析,需要的朋友參考下
1 . 用同一個(gè)類的源對(duì)象構(gòu)造一個(gè)目標(biāo)對(duì)象時(shí),會(huì)調(diào)用拷貝構(gòu)造函數(shù)來(lái)構(gòu)造目標(biāo)對(duì)象,如果沒有定義拷貝構(gòu)造函數(shù),將調(diào)用類的默認(rèn)拷貝函數(shù)來(lái)構(gòu)造目標(biāo)對(duì)象。
2 . 當(dāng)一個(gè)函數(shù)的返回值為一個(gè)類的對(duì)象時(shí),如果在調(diào)用函數(shù)中,沒有定義一個(gè)對(duì)象來(lái)接收這個(gè)返回對(duì)象值,會(huì)用返回一個(gè)臨時(shí)對(duì)象保存返回對(duì)象的值。在被調(diào)用函數(shù)結(jié)束時(shí),這個(gè)臨時(shí)對(duì)象被銷毀。而當(dāng)調(diào)用函數(shù)中有一個(gè)接受對(duì)象時(shí),就將返回對(duì)象賦值給接收對(duì)象,這個(gè)返回對(duì)象在調(diào)用函數(shù)結(jié)束時(shí)調(diào)用析構(gòu)函數(shù)。
3. 當(dāng)類有一個(gè)帶有一個(gè)參數(shù)的構(gòu)造函數(shù)時(shí),可以用這個(gè)參數(shù)同類型的數(shù)據(jù)初始化這個(gè)對(duì)象,默認(rèn)會(huì)調(diào)用這個(gè)構(gòu)造函數(shù)。
#include "stdafx.h"
#include <iostream>
using namespace std;
class B
{
public:
B():data(0) //默認(rèn)構(gòu)造函數(shù)
{
cout << "Default constructor is called." << endl;
}
B(int i):data(i) //帶參數(shù)的構(gòu)造函數(shù)
{
cout << "Constructor is called." << data << endl;
}
B(B &b) // 復(fù)制(拷貝)構(gòu)造函數(shù)
{
data = b.data; cout << "Copy Constructor is called." << data << endl;
}
B& operator = (const B &b) //賦值運(yùn)算符的重載
{
this->data = b.data;
cout << "The operator \"= \" is called." << data << endl;
return *this;
}
~B() //析構(gòu)函數(shù)
{
cout << "Destructor is called. " << data << endl;
}
private:
int data;
};
//函數(shù),參數(shù)是一個(gè)B類型對(duì)象,返回值也是一個(gè)B類型的對(duì)象
B fun(B b)
{
return b;
}
//測(cè)試函數(shù)
int _tmain(int argc, _TCHAR* argv[])
{
fun(1);
cout << endl;
B t1 = fun(2);
cout << endl;
B t2;
t2 = fun(3);
return 0;
}
輸出結(jié)果為:
Constructor is called.1 //用1構(gòu)造參數(shù)b
Copy Constructor is called.1 //用b拷貝構(gòu)造一個(gè)臨時(shí)對(duì)象,因?yàn)榇藭r(shí)沒有對(duì)象來(lái)接受fun的返回值
Destructor is called. 1 //參數(shù)b被析構(gòu)
Destructor is called. 1 //臨時(shí)對(duì)象被析構(gòu)
Constructor is called.2 //用2構(gòu)造參數(shù)b
Copy Constructor is called.2 //用b拷貝構(gòu)造t1,此時(shí)調(diào)用的是拷貝構(gòu)造函數(shù)
Destructor is called. 2 //參數(shù)b被析構(gòu)
Default constructor is called. //調(diào)用默認(rèn)的構(gòu)造函數(shù)構(gòu)造t2
Constructor is called.3 //用3構(gòu)造參數(shù)b
Copy Constructor is called.3 //用b拷貝構(gòu)造一個(gè)臨時(shí)對(duì)象
Destructor is called. 3 //參數(shù)b被析構(gòu)
The operator "= " is called.3 //調(diào)用=操作符初始化t2,此時(shí)調(diào)用的是賦值操作符
Destructor is called. 3 //臨時(shí)對(duì)象被析構(gòu)
Destructor is called. 3 //t2被析構(gòu)
Destructor is called. 2 //t1被析構(gòu)
請(qǐng)按任意鍵繼續(xù). . .
另外:
B t1 = fun(2); 和 B t2; t2 = fun(3); 調(diào)用的構(gòu)造函數(shù)不同,前面調(diào)用的是拷貝構(gòu)造函數(shù),后面的調(diào)用的是“=”操作符的重載,誰(shuí)能告訴我原因呢 ?
2 . 當(dāng)一個(gè)函數(shù)的返回值為一個(gè)類的對(duì)象時(shí),如果在調(diào)用函數(shù)中,沒有定義一個(gè)對(duì)象來(lái)接收這個(gè)返回對(duì)象值,會(huì)用返回一個(gè)臨時(shí)對(duì)象保存返回對(duì)象的值。在被調(diào)用函數(shù)結(jié)束時(shí),這個(gè)臨時(shí)對(duì)象被銷毀。而當(dāng)調(diào)用函數(shù)中有一個(gè)接受對(duì)象時(shí),就將返回對(duì)象賦值給接收對(duì)象,這個(gè)返回對(duì)象在調(diào)用函數(shù)結(jié)束時(shí)調(diào)用析構(gòu)函數(shù)。
3. 當(dāng)類有一個(gè)帶有一個(gè)參數(shù)的構(gòu)造函數(shù)時(shí),可以用這個(gè)參數(shù)同類型的數(shù)據(jù)初始化這個(gè)對(duì)象,默認(rèn)會(huì)調(diào)用這個(gè)構(gòu)造函數(shù)。
復(fù)制代碼 代碼如下:
#include "stdafx.h"
#include <iostream>
using namespace std;
class B
{
public:
B():data(0) //默認(rèn)構(gòu)造函數(shù)
{
cout << "Default constructor is called." << endl;
}
B(int i):data(i) //帶參數(shù)的構(gòu)造函數(shù)
{
cout << "Constructor is called." << data << endl;
}
B(B &b) // 復(fù)制(拷貝)構(gòu)造函數(shù)
{
data = b.data; cout << "Copy Constructor is called." << data << endl;
}
B& operator = (const B &b) //賦值運(yùn)算符的重載
{
this->data = b.data;
cout << "The operator \"= \" is called." << data << endl;
return *this;
}
~B() //析構(gòu)函數(shù)
{
cout << "Destructor is called. " << data << endl;
}
private:
int data;
};
//函數(shù),參數(shù)是一個(gè)B類型對(duì)象,返回值也是一個(gè)B類型的對(duì)象
B fun(B b)
{
return b;
}
//測(cè)試函數(shù)
int _tmain(int argc, _TCHAR* argv[])
{
fun(1);
cout << endl;
B t1 = fun(2);
cout << endl;
B t2;
t2 = fun(3);
return 0;
}
復(fù)制代碼 代碼如下:
輸出結(jié)果為:
復(fù)制代碼 代碼如下:
Constructor is called.1 //用1構(gòu)造參數(shù)b
Copy Constructor is called.1 //用b拷貝構(gòu)造一個(gè)臨時(shí)對(duì)象,因?yàn)榇藭r(shí)沒有對(duì)象來(lái)接受fun的返回值
Destructor is called. 1 //參數(shù)b被析構(gòu)
Destructor is called. 1 //臨時(shí)對(duì)象被析構(gòu)
Constructor is called.2 //用2構(gòu)造參數(shù)b
Copy Constructor is called.2 //用b拷貝構(gòu)造t1,此時(shí)調(diào)用的是拷貝構(gòu)造函數(shù)
Destructor is called. 2 //參數(shù)b被析構(gòu)
Default constructor is called. //調(diào)用默認(rèn)的構(gòu)造函數(shù)構(gòu)造t2
Constructor is called.3 //用3構(gòu)造參數(shù)b
Copy Constructor is called.3 //用b拷貝構(gòu)造一個(gè)臨時(shí)對(duì)象
Destructor is called. 3 //參數(shù)b被析構(gòu)
The operator "= " is called.3 //調(diào)用=操作符初始化t2,此時(shí)調(diào)用的是賦值操作符
Destructor is called. 3 //臨時(shí)對(duì)象被析構(gòu)
Destructor is called. 3 //t2被析構(gòu)
Destructor is called. 2 //t1被析構(gòu)
請(qǐng)按任意鍵繼續(xù). . .
另外:
B t1 = fun(2); 和 B t2; t2 = fun(3); 調(diào)用的構(gòu)造函數(shù)不同,前面調(diào)用的是拷貝構(gòu)造函數(shù),后面的調(diào)用的是“=”操作符的重載,誰(shuí)能告訴我原因呢 ?
您可能感興趣的文章:
- C++類成員構(gòu)造函數(shù)和析構(gòu)函數(shù)順序示例詳細(xì)講解
- 深入解析C++中的構(gòu)造函數(shù)和析構(gòu)函數(shù)
- C++中拷貝構(gòu)造函數(shù)的總結(jié)詳解
- c++類構(gòu)造函數(shù)詳解
- c++中拷貝構(gòu)造函數(shù)的參數(shù)類型必須是引用
- 解析C++中構(gòu)造函數(shù)的默認(rèn)參數(shù)和構(gòu)造函數(shù)的重載
- C++中拷貝構(gòu)造函數(shù)的應(yīng)用詳解
- C++11委托構(gòu)造函數(shù)和繼承構(gòu)造函數(shù)的實(shí)現(xiàn)
相關(guān)文章
C++數(shù)據(jù)結(jié)構(gòu)之鏈表的創(chuàng)建
這篇文章主要介紹了C++數(shù)據(jù)結(jié)構(gòu)之鏈表的創(chuàng)建的相關(guān)資料,希望通過(guò)本文幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10GCC編譯過(guò)程(預(yù)處理,編譯,匯編,鏈接)及GCC命令詳解
文章詳細(xì)介紹了GCC編譯器的工作原理,包括預(yù)處理、編譯、匯編和鏈接四個(gè)主要階段,每個(gè)階段都有其特定的任務(wù)和輸出文件,文章還解釋了如何使用GCC命令選項(xiàng)來(lái)查看每個(gè)階段的輸出,以及如何通過(guò)調(diào)整編譯選項(xiàng)來(lái)優(yōu)化程序性能或調(diào)試問(wèn)題,感興趣的朋友跟隨小編一起看看吧2024-11-11C語(yǔ)言數(shù)學(xué)問(wèn)題與簡(jiǎn)單DP01背包問(wèn)題詳解
這篇文章主要介紹了C語(yǔ)言數(shù)學(xué)問(wèn)題買不到的數(shù)目、螞蟻感冒、飲料換購(gòu)與簡(jiǎn)單DP01背包問(wèn)題的解決,屬于藍(lán)橋杯省賽中的題目,感興趣的同學(xué)來(lái)看看吧2022-04-04關(guān)于C++ string和c類型字符數(shù)組的對(duì)比
下面小編就為大家?guī)?lái)一篇關(guān)于C++ string和c類型字符數(shù)組的對(duì)比。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07