C++中的6種構(gòu)造函數(shù)舉例詳解
在 C++ 中,構(gòu)造函數(shù)是一種特殊的成員函數(shù),用于初始化類對象。在對象創(chuàng)建時(shí)自動(dòng)調(diào)用,構(gòu)造函數(shù)的主要作用是分配資源、初始化數(shù)據(jù)成員等。根據(jù)不同的功能和使用場景,C++ 提供了多種類型的構(gòu)造函數(shù):
1. 默認(rèn)構(gòu)造函數(shù) (Default Constructor)
默認(rèn)構(gòu)造函數(shù)不接受任何參數(shù),或者所有參數(shù)都有默認(rèn)值。當(dāng)你創(chuàng)建對象時(shí)如果不指定參數(shù),就會(huì)調(diào)用默認(rèn)構(gòu)造函數(shù)。
特點(diǎn):
- 如果類沒有定義任何構(gòu)造函數(shù),編譯器會(huì)自動(dòng)生成一個(gè)默認(rèn)構(gòu)造函數(shù)。
- 如果類有其他構(gòu)造函數(shù)但沒有顯式定義默認(rèn)構(gòu)造函數(shù),編譯器不會(huì)生成默認(rèn)構(gòu)造函數(shù)。
示例:
class MyClass {
public:
MyClass() { // 默認(rèn)構(gòu)造函數(shù)
x = 0;
}
private:
int x;
};
MyClass obj; // 自動(dòng)調(diào)用默認(rèn)構(gòu)造函數(shù)2. 參數(shù)化構(gòu)造函數(shù) (Parameterized Constructor)
參數(shù)化構(gòu)造函數(shù)允許在創(chuàng)建對象時(shí)傳遞參數(shù),用于初始化對象的成員變量。
特點(diǎn):
- 通過傳遞參數(shù),可以靈活地為對象賦值。
示例:
class MyClass {
public:
MyClass(int val) { // 參數(shù)化構(gòu)造函數(shù)
x = val;
}
private:
int x;
};
MyClass obj(10); // 使用參數(shù)化構(gòu)造函數(shù)3. 拷貝構(gòu)造函數(shù) (Copy Constructor)
拷貝構(gòu)造函數(shù)用于創(chuàng)建對象時(shí),用一個(gè)已存在的對象來初始化新對象。其形式為接受一個(gè)對象的常量引用。
特點(diǎn):
- 如果沒有顯式定義,編譯器會(huì)自動(dòng)生成一個(gè)默認(rèn)的拷貝構(gòu)造函數(shù)。
- 主要用于復(fù)制對象的值,特別是對于動(dòng)態(tài)分配內(nèi)存的類,手動(dòng)定義拷貝構(gòu)造函數(shù)可以防止淺拷貝問題。
示例:
class MyClass {
public:
MyClass(int val) : x(val) {} // 參數(shù)化構(gòu)造函數(shù)
MyClass(const MyClass &obj) { // 拷貝構(gòu)造函數(shù)
x = obj.x;
}
private:
int x;
};
MyClass obj1(10);
MyClass obj2 = obj1; // 使用拷貝構(gòu)造函數(shù)4. 移動(dòng)構(gòu)造函數(shù) (Move Constructor)
移動(dòng)構(gòu)造函數(shù)是在 C++11 引入的,用于通過"移動(dòng)語義"來避免拷貝操作,從而提高程序的性能,特別是涉及動(dòng)態(tài)分配資源的對象。
特點(diǎn):
- 接受一個(gè)右值引用 (
T&&)。 - 用于將資源從一個(gè)臨時(shí)對象“移動(dòng)”到新的對象中,通常通過偷取資源而不是復(fù)制它們。
示例:
class MyClass {
public:
MyClass(int val) : x(new int(val)) {} // 動(dòng)態(tài)分配內(nèi)存
MyClass(MyClass&& obj) noexcept { // 移動(dòng)構(gòu)造函數(shù)
x = obj.x;
obj.x = nullptr; // 釋放臨時(shí)對象的所有權(quán)
}
~MyClass() { delete x; }
private:
int* x;
};
MyClass obj1(10);
MyClass obj2 = std::move(obj1); // 使用移動(dòng)構(gòu)造函數(shù)5. 委托構(gòu)造函數(shù) (Delegating Constructor)
委托構(gòu)造函數(shù)是在一個(gè)構(gòu)造函數(shù)中調(diào)用同一個(gè)類的另一個(gè)構(gòu)造函數(shù),從而避免代碼重復(fù)。這是 C++11 引入的特性。
特點(diǎn):
- 可以簡化多個(gè)構(gòu)造函數(shù)之間的代碼邏輯,避免重復(fù)代碼。
示例:
class MyClass {
public:
MyClass() : MyClass(0) { // 委托給參數(shù)化構(gòu)造函數(shù)
// 可以額外執(zhí)行一些操作
}
MyClass(int val) : x(val) {} // 參數(shù)化構(gòu)造函數(shù)
private:
int x;
};
MyClass obj; // 調(diào)用默認(rèn)構(gòu)造函數(shù),委托給參數(shù)化構(gòu)造函數(shù)6. 顯式構(gòu)造函數(shù) (Explicit Constructor)
explicit 構(gòu)造函數(shù)用于防止隱式類型轉(zhuǎn)換。這對防止錯(cuò)誤的自動(dòng)類型轉(zhuǎn)換特別有用。
特點(diǎn):
- 防止構(gòu)造函數(shù)被自動(dòng)調(diào)用進(jìn)行隱式轉(zhuǎn)換。
示例:
class MyClass {
public:
explicit MyClass(int val) : x(val) {} // 顯式構(gòu)造函數(shù)
private:
int x;
};
MyClass obj1(10); // OK
MyClass obj2 = 10; // 錯(cuò)誤,顯式構(gòu)造函數(shù)禁止隱式轉(zhuǎn)換7. 析構(gòu)函數(shù) (Destructor)
雖然析構(gòu)函數(shù)不算構(gòu)造函數(shù),但與其作用類似。析構(gòu)函數(shù)用于在對象生命周期結(jié)束時(shí)釋放資源。析構(gòu)函數(shù)沒有參數(shù),且前面有 ~ 符號。
示例:
class MyClass {
public:
MyClass() { x = new int(10); }
~MyClass() { delete x; } // 析構(gòu)函數(shù)
private:
int* x;
};總結(jié):
C++ 中構(gòu)造函數(shù)的類型和用途可以總結(jié)如下:
- 默認(rèn)構(gòu)造函數(shù): 初始化對象,通常不需要參數(shù)。
- 參數(shù)化構(gòu)造函數(shù): 通過傳參初始化對象的成員。
- 拷貝構(gòu)造函數(shù): 通過已有對象初始化新對象。
- 移動(dòng)構(gòu)造函數(shù): 移動(dòng)對象的資源,避免不必要的拷貝。
- 委托構(gòu)造函數(shù): 在一個(gè)構(gòu)造函數(shù)中調(diào)用另一個(gè)構(gòu)造函數(shù)。
- 顯式構(gòu)造函數(shù): 防止隱式類型轉(zhuǎn)換。
這些構(gòu)造函數(shù)為對象的初始化提供了靈活的選擇,尤其在管理資源時(shí),合理使用拷貝和移動(dòng)構(gòu)造函數(shù)可以顯著提升程序的效率。
到此這篇關(guān)于C++中6種構(gòu)造函數(shù)的文章就介紹到這了,更多相關(guān)C++構(gòu)造函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Cocos2d-x UI開發(fā)之CCControlSlider控件類使用實(shí)例
這篇文章主要介紹了Cocos2d-x UI開發(fā)之CCControlSlider控件類使用實(shí)例,本文代碼中包含大量注釋講解了CCControlSlider控件類的使用,需要的朋友可以參考下2014-09-09
C/C++實(shí)現(xiàn)高并發(fā)http服務(wù)器的代碼示例
這篇文章簡單給大家介紹了C/C++實(shí)現(xiàn)高并發(fā)http服務(wù)器的代碼示例,文章通過代碼和圖文介紹的非常詳細(xì),感興趣的同學(xué)可以參考閱讀2023-07-07

