首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> C++ 基礎

C++ 建構函數完整指南

     編輯:C++ 基礎

建構函數:物件的「出生證明」

特殊成員函數,與類別同名,無回傳型別(連 void 也不可),物件建立時自動呼叫,完成初始化職責。

基本語法與呼叫

class Student {
    const char* m_name;
    int m_age;
    float m_score;
public:
    Student(const char* name, int age, float score); // 宣告
    void show() const;
};

Student::Student(const char* name, int age, float score)
    : m_name(name), m_age(age), m_score(score) { } // 定義

int main() {
    Student stu("小明", 15, 92.5f);        // 棧上:參數跟在物件名後
    Student* p = new Student("李華", 16, 96); // 堆上:參數跟在類別名後
    stu.show(); p->show();
    delete p;
    return 0;
}

必須為 public,否則無法建立物件(private/protected 僅用於單例/工廠等進階模式)。

建構函數重載:多樣化的初始化路徑

class Student {
    const char* m_name;
    int m_age;
    float m_score;
public:
    Student();                               // 預設建構
    Student(const char* name, int age, float score); // 完整初始化
    void setName(const char* name);
    void setAge(int age);
    void setScore(float score);
    void show() const;
};

Student::Student() : m_name(nullptr), m_age(0), m_score(0.0f) { }
Student::Student(const char* name, int age, float score)
    : m_name(name), m_age(age), m_score(score) { }
  • 重載規則:參數列表不同(個數、型別、順序)。
  • 建立物件時**必須**匹配某個建構函數;若類別有使用者定義建構函數,編譯器**不再**合成預設建構函數。
  • Student stu; 或 new Student 僅在存在零參數建構函數時合法。

預設建構函數:編譯器的隱性禮物

  • 使用者**未**定義任何建構函數 → 編譯器合成 Student() { }(空函數體)。
  • 使用者定義了**任意**建構函數 → 編譯器**不**合成預設版本。
  • 實際上合成版本會初始化基類、成員物件,非真正空體,但初學者可視為空體理解。

呼叫零參數建構函數的括號省略規則

Student s1;           // OK:呼叫 Student()
Student s2();         // 錯誤:被解析為函數宣告!
Student* p1 = new Student;     // OK
Student* p2 = new Student();   // OK

陷阱:Student s2(); 宣告一個回傳 Student 的函數,而非建立物件。棧上建立物件**不可加括號**,堆上則可加可不加。

實戰範例:動態陣列封裝(RAII 前身)

class IntArray {
    int* m_data;
    int m_size;
public:
    IntArray(int size) : m_size(size) {
        m_data = (size > 0) ? new int[size] : nullptr;
    }
    ~IntArray() { delete[] m_data; }
    int& operator[](int i) { return m_data[i]; }
    int size() const { return m_size; }
};

建構函數配置資源,析構函數釋放資源,物件生命週期綁定資源生命週期,這是 C++ 資源管理的核心範式。

常見錯誤

錯誤後果修正
建構函數回傳值編譯錯誤移除回傳型別
主體內 return value;編譯錯誤建構函數不可回傳值
未定義建構函數卻傳參建立物件編譯錯誤提供對應參數的建構函數
定義建構函數後預期編譯器仍提供預設版本無參數建立物件失敗手動補上 Student() 或使用預設參數
Student s(); 以為建立物件宣告函數改為 Student s;

總結

建構函數是物件初始化的強制入口。掌握「重載多樣化初始化」、「預設建構函數合成規則」、「括號省略陷阱」,並配合初始化串列、RAII 模式,即能編寫安全、高效的 C++ 類別。

  1. 上一篇文章:
  2. 下一篇文章:
  • --热点文章
  • Copyright © 程式碼之家 All Rights Reserved