1. 程式人生 > >C++ 類的賦值運算子'='過載

C++ 類的賦值運算子'='過載

參考

什麼類需要過載賦值運算子

先來看一個普通類的直接賦值。

#include <iostream>
using namespace std;

class person{
    int age;
public:
    person(const int& a=10):age(a){}  //建構函式
    ~person();  //解構函式
    void showAdd();   //列印age的地址
};

person::~person(){cout<<"析構\n";}

void person::showAdd() {cout <<hex<< &age<<endl;}

int main() {
    person a(11);
    person b;
    b = a;
    a.showAdd();
    b.showAdd();
    return 0;
}
/*
結果是:
0x7fffffffdc5c
0x7fffffffdc60
析構
析構
*/

這是這個程式的記憶體情況,一切都執行的很正常,不需要運算子過載。

看下邊這個例子,這個類的建構函式需要申請(new)堆記憶體:

#include <iostream>
using namespace std;

class person{
    int* age;
public:
    person(const int& a=10);  //建構函式
    ~person();  //解構函式
    void showAdd();   //列印age的地址
    void show();  //列印age指向的值
    void set(const int& a){*age=a;}
};

person::person(const int& a) {age = new int(a);}

person::~person(){delete age; cout<<"析構\n";}

void person::showAdd() {cout << hex << age<<endl;}

void person::show() {cout<<*age<<endl;}

void f(person& a) {
    person b;
    b=a;
    a.show();
    b.show();
    a.showAdd();
    b.showAdd();
    //因為b是區域性變數,所以進入main函式之前,b會自動呼叫解構函式
}

int main() {
    person a(11);
    f(a);
    cout<<"進入main函式\n";
    a.set(9);  //因為b已經釋放過age指標,set應該會出錯
    a.show();
    return 0;
}

執行結果如下:

這是這個程式進入 f() 函式時的記憶體情況,兩個age指標指向同一塊記憶體。

這是這個程式退出 f() 函式進入main函式的情況,因為b是區域性變數,所以f()函式結束的時候,b會呼叫解構函式,釋放age指向的堆記憶體。這時候a.set()就會發生錯誤,因為記憶體已經釋放,無權修改記憶體裡的值。就算沒有set()函式,main函式結束的時候還會產生doublefree的錯誤,同一塊記憶體被釋放兩次,C++文件說明這是個未定義行為,所以不同編譯器可能處理手段不一樣,我的gcc 7.4.0 竟然沒有報錯。後來我又在網上的一些線上編譯器實驗一下,有的會報錯,有的不會。

所以結論就是:類的建構函式需要申請堆記憶體的時候,我們要進行賦值運算子的過載,下面講如何過載。

如何過載賦值運算子

#include <iostream>
using namespace std;

class person{
    int* age;
public:
    person(const int& a=10);  //建構函式
    ~person();  //解構函式
    void showAdd();   //列印age的地址
    void show();  //列印age指向的值
    void set(const int& a){*age=a;}  //設定age指向的值

    void operator=(person const& e);  //過載賦值運算子
};

void person::operator=(person const& e)
{
    if(age) delete age;  //如果原先age申請過堆記憶體,要先釋放
    int data = *(e.age);
    age = new int(data);
}

person::person(const int& a) {age = new int(a);}

person::~person(){delete age; cout<<"析構\n";}

void person::showAdd() {cout << hex << age<<endl;}

void person::show() {cout<<*age<<endl;}

void f(person& a) {
    person b;
    b = a;  //這時候b指向了一塊新的空間
    a.show();
    b.show();
    a.showAdd();
    b.showAdd();
    //因為b是區域性變數,所以進入main函式之前,b會自動呼叫解構函式
}

int main() {
    person a(11);
    f(a);
    cout<<"進入main函式\n";
    a.set(9);  //因為b釋放的指標和age指向不一樣,set不會出錯
    return 0;
}

程式執行正常,記憶體圖如下:

注意上邊我用的operator=返回值是void, 這樣不能進行連續賦值,比如:person a = b = c; ,若想連續賦值,返回值要宣告為 引用

person& person::operator=(person const& e)
{
    if(age) delete age; 
    int data = *(e.age);
    age = new int(data);
    return *this;
}

關於拷貝函式

再回看一下上邊的程式碼,我的宣告語句和賦值語句是分開的person b; b=a;,如果宣告時賦值person b=a;,那麼呼叫的函式就不是operator=了,而是拷貝函式

class person{
    int* age;
public:
    person(person const& e);  //這就是拷貝函式 
}

需要注意的是: 上邊說的operator返回值有兩種情況:void和引用,其實還有第三種,既然能返回引用那就還能返回值:

person person::operator=(person const& e)
{
    if(age) delete age; 
    int data = *(e.age);
    age = new int(data);
    return *this;
}

函式返回值的時候會臨時構造一個person變數, 這個變數的age的指向和呼叫operator=的物件的age指向一樣,也就是:

operator=呼叫完之後,臨時變數會呼叫解構函式,從而導致和上邊一樣的錯誤,doublefree。所以operator=的返回值最好是引用