C++ 類的賦值運算符''=''重載的方法實現(xiàn)
什么類需要重載賦值運算符
先來看一個普通類的直接賦值。
#include <iostream>
using namespace std;
class person{
int age;
public:
person(const int& a=10):age(a){} //構造函數(shù)
~person(); //析構函數(shù)
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
析構
析構
*/

這是這個程序的內(nèi)存情況,一切都運行的很正常,不需要運算符重載。
看下邊這個例子,這個類的構造函數(shù)需要申請(new)堆內(nèi)存:
#include <iostream>
using namespace std;
class person{
int* age;
public:
person(const int& a=10); //構造函數(shù)
~person(); //析構函數(shù)
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函數(shù)之前,b會自動調(diào)用析構函數(shù)
}
int main() {
person a(11);
f(a);
cout<<"進入main函數(shù)\n";
a.set(9); //因為b已經(jīng)釋放過age指針,set應該會出錯
a.show();
return 0;
}
運行結果如下:

這是這個程序進入 f() 函數(shù)時的內(nèi)存情況,兩個age指針指向同一塊內(nèi)存。

這是這個程序退出 f() 函數(shù)進入main函數(shù)的情況,因為b是局部變量,所以f()函數(shù)結束的時候,b會調(diào)用析構函數(shù),釋放age指向的堆內(nèi)存。這時候a.set()就會發(fā)生錯誤,因為內(nèi)存已經(jīng)釋放,無權修改內(nèi)存里的值。就算沒有set()函數(shù),main函數(shù)結束的時候還會產(chǎn)生doublefree的錯誤,同一塊內(nèi)存被釋放兩次,C++文檔說明這是個未定義行為,所以不同編譯器可能處理手段不一樣,我的gcc 7.4.0 竟然沒有報錯。后來我又在網(wǎng)上的一些在線編譯器實驗一下,有的會報錯,有的不會。
所以結論就是:類的構造函數(shù)需要申請堆內(nèi)存的時候,我們要進行賦值運算符的重載,下面講如何重載。
如何重載賦值運算符
#include <iostream>
using namespace std;
class person{
int* age;
public:
person(const int& a=10); //構造函數(shù)
~person(); //析構函數(shù)
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申請過堆內(nèi)存,要先釋放
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函數(shù)之前,b會自動調(diào)用析構函數(shù)
}
int main() {
person a(11);
f(a);
cout<<"進入main函數(shù)\n";
a.set(9); //因為b釋放的指針和age指向不一樣,set不會出錯
return 0;
}

程序運行正常,內(nèi)存圖如下:

注意上邊我用的operator=返回值是void, 這樣不能進行連續(xù)賦值,比如: person a = b = c; ,若想連續(xù)賦值,返回值要聲明為 引用
person& person::operator=(person const& e)
{
if(age) delete age;
int data = *(e.age);
age = new int(data);
return *this;
}
關于拷貝函數(shù)
再回看一下上邊的代碼,我的聲明語句和賦值語句是分開的 person b; b=a; ,如果聲明時賦值 person b=a; ,那么調(diào)用的函數(shù)就不是 operator= 了,而是拷貝函數(shù)
class person{
int* age;
public:
person(person const& e); //這就是拷貝函數(shù)
}
需要注意的是:上邊說的operator返回值有兩種情況:void和引用,其實還有第三種,既然能返回引用那就還能返回值:
person person::operator=(person const& e)
{
if(age) delete age;
int data = *(e.age);
age = new int(data);
return *this;
}
函數(shù)返回值的時候會臨時構造一個 person 變量, 這個變量的 age 的指向和調(diào)用 operator= 的對象的 age 指向一樣,也就是:

當 operator= 調(diào)用完之后,臨時變量會調(diào)用析構函數(shù),從而導致和上邊一樣的錯誤,doublefree。所以 operator= 的返回值最好是引用!
到此這篇關于C++ 類的賦值運算符'='重載的方法實現(xiàn)的文章就介紹到這了,更多相關C++ 類的賦值運算符'='重載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于MATLAB神經(jīng)網(wǎng)絡圖像識別的高識別率代碼
今天小編就為大家分享一篇關于基于MATLAB神經(jīng)網(wǎng)絡圖像識別的高識別率代碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
C++ min/max_element 函數(shù)用法詳解
這篇文章主要介紹了C++ min/max_element 函數(shù)用法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

