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=的返回值最好是引用!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM