深入C++中構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、賦值操作符、析構(gòu)函數(shù)的調(diào)用過程總結(jié)
更新時間:2013年05月29日 10:16:48 作者:
本篇文章是對C++中構(gòu)造函數(shù)、拷貝構(gòu)造函數(shù)、賦值操作符、析構(gòu)函數(shù)的調(diào)用過程進行了總結(jié)與分析,需要的朋友參考下
1 . 用同一個類的源對象構(gòu)造一個目標(biāo)對象時,會調(diào)用拷貝構(gòu)造函數(shù)來構(gòu)造目標(biāo)對象,如果沒有定義拷貝構(gòu)造函數(shù),將調(diào)用類的默認(rèn)拷貝函數(shù)來構(gòu)造目標(biāo)對象。
2 . 當(dāng)一個函數(shù)的返回值為一個類的對象時,如果在調(diào)用函數(shù)中,沒有定義一個對象來接收這個返回對象值,會用返回一個臨時對象保存返回對象的值。在被調(diào)用函數(shù)結(jié)束時,這個臨時對象被銷毀。而當(dāng)調(diào)用函數(shù)中有一個接受對象時,就將返回對象賦值給接收對象,這個返回對象在調(diào)用函數(shù)結(jié)束時調(diào)用析構(gòu)函數(shù)。
3. 當(dāng)類有一個帶有一個參數(shù)的構(gòu)造函數(shù)時,可以用這個參數(shù)同類型的數(shù)據(jù)初始化這個對象,默認(rèn)會調(diào)用這個構(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) //賦值運算符的重載
{
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ù)是一個B類型對象,返回值也是一個B類型的對象
B fun(B b)
{
return b;
}
//測試函數(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)造一個臨時對象,因為此時沒有對象來接受fun的返回值
Destructor is called. 1 //參數(shù)b被析構(gòu)
Destructor is called. 1 //臨時對象被析構(gòu)
Constructor is called.2 //用2構(gòu)造參數(shù)b
Copy Constructor is called.2 //用b拷貝構(gòu)造t1,此時調(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)造一個臨時對象
Destructor is called. 3 //參數(shù)b被析構(gòu)
The operator "= " is called.3 //調(diào)用=操作符初始化t2,此時調(diào)用的是賦值操作符
Destructor is called. 3 //臨時對象被析構(gòu)
Destructor is called. 3 //t2被析構(gòu)
Destructor is called. 2 //t1被析構(gòu)
請按任意鍵繼續(xù). . .
另外:
B t1 = fun(2); 和 B t2; t2 = fun(3); 調(diào)用的構(gòu)造函數(shù)不同,前面調(diào)用的是拷貝構(gòu)造函數(shù),后面的調(diào)用的是“=”操作符的重載,誰能告訴我原因呢 ?
2 . 當(dāng)一個函數(shù)的返回值為一個類的對象時,如果在調(diào)用函數(shù)中,沒有定義一個對象來接收這個返回對象值,會用返回一個臨時對象保存返回對象的值。在被調(diào)用函數(shù)結(jié)束時,這個臨時對象被銷毀。而當(dāng)調(diào)用函數(shù)中有一個接受對象時,就將返回對象賦值給接收對象,這個返回對象在調(diào)用函數(shù)結(jié)束時調(diào)用析構(gòu)函數(shù)。
3. 當(dāng)類有一個帶有一個參數(shù)的構(gòu)造函數(shù)時,可以用這個參數(shù)同類型的數(shù)據(jù)初始化這個對象,默認(rèn)會調(diào)用這個構(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) //賦值運算符的重載
{
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ù)是一個B類型對象,返回值也是一個B類型的對象
B fun(B b)
{
return b;
}
//測試函數(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)造一個臨時對象,因為此時沒有對象來接受fun的返回值
Destructor is called. 1 //參數(shù)b被析構(gòu)
Destructor is called. 1 //臨時對象被析構(gòu)
Constructor is called.2 //用2構(gòu)造參數(shù)b
Copy Constructor is called.2 //用b拷貝構(gòu)造t1,此時調(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)造一個臨時對象
Destructor is called. 3 //參數(shù)b被析構(gòu)
The operator "= " is called.3 //調(diào)用=操作符初始化t2,此時調(diào)用的是賦值操作符
Destructor is called. 3 //臨時對象被析構(gòu)
Destructor is called. 3 //t2被析構(gòu)
Destructor is called. 2 //t1被析構(gòu)
請按任意鍵繼續(xù). . .
另外:
B t1 = fun(2); 和 B t2; t2 = fun(3); 調(diào)用的構(gòu)造函數(shù)不同,前面調(diào)用的是拷貝構(gòu)造函數(shù),后面的調(diào)用的是“=”操作符的重載,誰能告訴我原因呢 ?
相關(guān)文章
C++實現(xiàn)數(shù)字轉(zhuǎn)換為十六進制字符串的方法
這篇文章主要介紹了C++實現(xiàn)數(shù)字轉(zhuǎn)換為十六進制字符串的方法,涉及C++操作數(shù)字與字符串轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06VS2022連接sqlserver數(shù)據(jù)庫教程
本文主要介紹了VS2022連接sqlserver數(shù)據(jù)庫教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07C語言?模擬實現(xiàn)memcpy與memmove函數(shù)詳解
這篇文章主要介紹了C語言詳解如何模擬內(nèi)存函數(shù),用到了mencpy與memmove兩個函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-04-04C語言實現(xiàn)班級檔案管理系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)班級檔案管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12