深入C++中構造函數(shù)、拷貝構造函數(shù)、賦值操作符、析構函數(shù)的調用過程總結
1 . 用同一個類的源對象構造一個目標對象時,會調用拷貝構造函數(shù)來構造目標對象,如果沒有定義拷貝構造函數(shù),將調用類的默認拷貝函數(shù)來構造目標對象。
2 . 當一個函數(shù)的返回值為一個類的對象時,如果在調用函數(shù)中,沒有定義一個對象來接收這個返回對象值,會用返回一個臨時對象保存返回對象的值。在被調用函數(shù)結束時,這個臨時對象被銷毀。而當調用函數(shù)中有一個接受對象時,就將返回對象賦值給接收對象,這個返回對象在調用函數(shù)結束時調用析構函數(shù)。
3. 當類有一個帶有一個參數(shù)的構造函數(shù)時,可以用這個參數(shù)同類型的數(shù)據(jù)初始化這個對象,默認會調用這個構造函數(shù)。
#include "stdafx.h"
#include <iostream>
using namespace std;
class B
{
public:
B():data(0) //默認構造函數(shù)
{
cout << "Default constructor is called." << endl;
}
B(int i):data(i) //帶參數(shù)的構造函數(shù)
{
cout << "Constructor is called." << data << endl;
}
B(B &b) // 復制(拷貝)構造函數(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() //析構函數(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;
}
輸出結果為:
Constructor is called.1 //用1構造參數(shù)b
Copy Constructor is called.1 //用b拷貝構造一個臨時對象,因為此時沒有對象來接受fun的返回值
Destructor is called. 1 //參數(shù)b被析構
Destructor is called. 1 //臨時對象被析構
Constructor is called.2 //用2構造參數(shù)b
Copy Constructor is called.2 //用b拷貝構造t1,此時調用的是拷貝構造函數(shù)
Destructor is called. 2 //參數(shù)b被析構
Default constructor is called. //調用默認的構造函數(shù)構造t2
Constructor is called.3 //用3構造參數(shù)b
Copy Constructor is called.3 //用b拷貝構造一個臨時對象
Destructor is called. 3 //參數(shù)b被析構
The operator "= " is called.3 //調用=操作符初始化t2,此時調用的是賦值操作符
Destructor is called. 3 //臨時對象被析構
Destructor is called. 3 //t2被析構
Destructor is called. 2 //t1被析構
請按任意鍵繼續(xù). . .
另外:
B t1 = fun(2); 和 B t2; t2 = fun(3); 調用的構造函數(shù)不同,前面調用的是拷貝構造函數(shù),后面的調用的是“=”操作符的重載,誰能告訴我原因呢 ?
上一篇:海量數(shù)據(jù)處理系列之:用C++實現(xiàn)Bitmap算法
欄 目:C語言
下一篇:C++實現(xiàn)strcmp字符串比較的深入探討
本文標題:深入C++中構造函數(shù)、拷貝構造函數(shù)、賦值操作符、析構函數(shù)的調用過程總結
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/4421.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解約瑟夫環(huán)的數(shù)學優(yōu)化方法
- 01-10深入二叉樹兩個結點的最低共同父結點的詳解
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進程環(huán)境詳解


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結
- 08-05DEDE織夢data目錄下的sessions文件夾有什


