Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

C++ 類的建構函式與解構函式完全解析

建構函式

建構函式負責在物件誕生時完成成員的初始化。它與一般成員函式有幾點不同:

  • 名稱必須與類名相同;
  • 不能帶任何返回值(連 void 也不行)。

即使你不自行定義,編譯器也會為你合成一個預設建構函式,只是它什麼都不做,形如 stu::stu(void) {}。

解構函式

解構函式負責在物件消亡時回收資源(例如 malloc / new 取得的記憶體),會在物件生命週期結束時被自動呼叫,不需手動觸發。特色是:

  • 名稱為類名加上 ~;
  • 不能帶參數,也不能有返回值。

同樣地,不自行定義時編譯器會合成一個空的預設版本。

幾個容易踩到的細節

  1. 建構函式有三種常見呼叫方式:
    stu yl(1, "yanlei");            // 隱含呼叫並初始化
    stu yl;                          // 隱含呼叫(可能呼叫重載版本)
    stu yl = stu(1, "yanlei");       // 顯式呼叫並賦值
    注意 stu yl(void); 不是建立物件,而是一個回傳 stu 的函式原型。
  2. 建構函式不能有返回值;解構函式既不能有參數也不能有返回值。
  3. 物件初始化後仍可用建構函式重新賦值:
    stu yl = stu(1, "yanlei");
    yl = stu(1, "gaopeng");
    此時會先建立一塊暫時物件,把資料複製進 yl,再釋放暫時物件,因此會觸發一次解構函式呼叫。
  4. 名稱規則:建構 stu::stu(void),解構 stu::~stu(void)。

完整範例

// ct1.h
#include <iostream>
using namespace std;

typedef struct mem
{
    int        t_sorce_;
    const char *name_;
} MEM;

class stu
{
private:
    int id_;
    MEM st_;

public:
    stu(int id, const char *name);   // 建構函式
    stu(void);                       // 建構函式(重載)
    int  set_src(int id, int a, int b, const char *name);
    int  sh_src(void);
    const stu* check(const stu* info) const;
    ~stu(void);                      // 解構函式
};
// fu.cpp
#include <iostream>
#include "ct1.h"
using namespace std;

stu::stu(int id, const char *name)   // 建構函式,不可有返回值
{
    id_ = id;
    st_.name_ = name;
    st_.t_sorce_ = 0;
}

stu::stu(void)
{
    static char name[20] = "gaopeng";
    st_.name_ = name;
    id_ = 0;
    st_.t_sorce_ = 0;
}

stu::~stu(void)                      // 解構函式,不可有參數與返回值
{
    cout << "call destructor for name:" << st_.name_ << endl;
}

int stu::set_src(int id, int a, int b, const char *name)
{
    id_ = id;
    st_.t_sorce_ = a + b;
    st_.name_ = name;
    return 0;
}

int stu::sh_src(void)
{
    cout << id_ << endl;
    cout << st_.t_sorce_ << endl;
    cout << st_.name_ << endl;
    return 0;
}

const stu* stu::check(const stu* info) const
{
    if (info->id_ > id_)
        return info;
    else
        return this;   // this 指標
}
// main.cpp
#include <iostream>
#include "ct1.h"
using namespace std;

int main(void)
{
    {
        char a[20] = "yanlei";
        stu yl(1, a);          // 顯式初始化
        yl.sh_src();

        stu gp;                // 隱含呼叫 stu()
        gp.sh_src();
        gp.set_src(2, 10, 20, "gaopeng1");
        gp.sh_src();

        stu gp2 = stu(3, "gaopeng2");   // 初始化
        gp2 = stu(3, "gaopeng3");       // 建立暫時物件、複製、釋放 → 觸發解構

        gp2 = gp;
        gp2.sh_src();

        stu gp3;
        cout << "gp3 resource:" << endl;
        gp3.sh_src();

        gp3 = *(gp3.check(&gp2));
        cout << "gp3 after check:" << endl;
        gp3.sh_src();
    }
}

執行結果

1
0
yanlei
0
0
gaopeng
2
30
gaopeng1
call destructor for name:gaopeng3
2
30
gaopeng1
gp3 resource:
0
0
gaopeng
gp3 after check:
2
30
gaopeng1
call destructor for name:gaopeng1
call destructor for name:gaopeng1
call destructor for name:gaopeng1
call destructor for name:yanlei

注意那個 call destructor for name:gaopeng3,正是暫時物件釋放時觸發的解構呼叫;而最後連續幾次則是區塊結束時各物件依建立順序反向解構的結果。另外別忘了 check 中的 this 指標用法——它讓常函式能回傳自身,實現「取兩者中較大者」這類語意。

Copyright © Linux教程網 All Rights Reserved