C++超詳細講解操作符的重載
一、需要解決的問題
下面的復(fù)數(shù)解決方案是否可行?

下面看一下復(fù)數(shù)的加法操作:
#include <stdio.h>
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex Add(const Complex& p1, const Complex& p2);
};
Complex Add(const Complex& p1, const Complex& p2)
{
Complex ret;
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = Add(c1, c2); // c1 + c2
printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
return 0;
}輸出結(jié)果如下:

思考
Add 函數(shù)可以解決 Complex 對象相加的問題,但是 Complex 是現(xiàn)實世界中確實存在的復(fù)數(shù),并且復(fù)數(shù)在數(shù)學(xué)中的地位和普通的實數(shù)相同。
為什么不能讓+操作符也支持復(fù)數(shù)相加呢?這個就涉及到操作符的重載。
二、操作符重載
C++ 中的重載能夠擴展操作符的功能
操作符的重載以函數(shù)的方式進行
本質(zhì)
用特殊形式的函數(shù)擴展操作符的功能
- 通過 operator 關(guān)鍵字可以定義特殊的函數(shù)
- operator 的本質(zhì)是通過函數(shù)重載操作符
語法

下面來初探一下操作符重載:
#include <stdio.h>
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
friend Complex operator + (const Complex& p1, const Complex& p2);
};
Complex operator + (const Complex& p1, const Complex& p2)
{
Complex ret;
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2; // operator + (c1, c2)
printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
return 0;
}輸出結(jié)果如下:

可以將操作符重載函數(shù)定義為類的成員函數(shù)
- 比全局操作符重載函數(shù)少—個參數(shù)(左操作數(shù))
- 不需要依賴友元就可以完成操作符重載
- 編譯器優(yōu)先在成員函數(shù)中尋找操作符重載函數(shù)

下面來實現(xiàn)在成員函數(shù)中重載操作符:
#include <stdio.h>
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
Complex operator + (const Complex& p)
{
Complex ret;
printf("Complex operator + (const Complex& p)\n");
ret.a = this->a + p.a;
ret.b = this->b + p.b;
return ret;
}
friend Complex operator + (const Complex& p1, const Complex& p2);
};
Complex operator + (const Complex& p1, const Complex& p2)
{
Complex ret;
printf("Complex operator + (const Complex& p1, const Complex& p2)\n");
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b;
return ret;
}
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2; // c1.operator + (c2)
printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
return 0;
}輸出結(jié)果如下:

這個說明編譯器優(yōu)先在成員函數(shù)中尋找操作符重載函數(shù)
故上述代碼可以直接寫成:
#include <stdio.h>
class Complex
{
int a;
int b;
public:
Complex(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
Complex operator + (const Complex& p)
{
Complex ret;
ret.a = this->a + p.a;
ret.b = this->b + p.b;
return ret;
}
};
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2; // c1.operator + (c2)
printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
return 0;
}三、小結(jié)
- 操作符重載是 C++ 的強大特性之一
- 操作符重載的本質(zhì)是通過函數(shù)擴展操作符的功能
- operator 關(guān)鍵字是實現(xiàn)操作符重載的關(guān)鍵
- 操作符重載遵循相同的函數(shù)重載規(guī)則
- 全局函數(shù)和成員函數(shù)都可以實現(xiàn)對操作符的重載
到此這篇關(guān)于C++超詳細講解操作符的重載的文章就介紹到這了,更多相關(guān)C++操作符重載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++超詳細分析講解內(nèi)聯(lián)函數(shù)
為了消除函數(shù)調(diào)用的時空開銷,C++ 提供一種提高效率的方法,即在編譯時將函數(shù)調(diào)用處用函數(shù)體替換,類似于C語言中的宏展開。這種在函數(shù)調(diào)用處直接嵌入函數(shù)體的函數(shù)稱為內(nèi)聯(lián)函數(shù)(Inline Function),又稱內(nèi)嵌函數(shù)或者內(nèi)置函數(shù)2022-06-06
C++語言基礎(chǔ) this和static關(guān)鍵字
這篇文章主要介紹了C++語言基礎(chǔ) this和static關(guān)鍵字,需要的朋友可以參考下2020-01-01
win10+VS2017+Cuda10.0環(huán)境配置詳解
這篇文章主要介紹了win10+VS2017+Cuda10.0環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
C++實現(xiàn)LeetCode(93.復(fù)原IP地址)
這篇文章主要介紹了C++實現(xiàn)LeetCode(93.復(fù)原IP地址),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
C++基礎(chǔ)入門教程(九):函數(shù)指針之回調(diào)
這篇文章主要介紹了C++基礎(chǔ)入門教程(九):函數(shù)指針之回調(diào),本文講解了函數(shù)的地址、聲明函數(shù)指針、歷史原因、typedef挽救復(fù)雜的函數(shù)指針等內(nèi)容,需要的朋友可以參考下2014-11-11

