C++超詳細(xì)講解操作符的重載
一、需要解決的問(wèn)題
下面的復(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 對(duì)象相加的問(wèn)題,但是 Complex 是現(xiàn)實(shí)世界中確實(shí)存在的復(fù)數(shù),并且復(fù)數(shù)在數(shù)學(xué)中的地位和普通的實(shí)數(shù)相同。
為什么不能讓+操作符也支持復(fù)數(shù)相加呢?這個(gè)就涉及到操作符的重載。
二、操作符重載
C++ 中的重載能夠擴(kuò)展操作符的功能
操作符的重載以函數(shù)的方式進(jìn)行
本質(zhì)
用特殊形式的函數(shù)擴(kuò)展操作符的功能
- 通過(guò) operator 關(guān)鍵字可以定義特殊的函數(shù)
- operator 的本質(zhì)是通過(guò)函數(shù)重載操作符
語(yǔ)法

下面來(lái)初探一下操作符重載:
#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ù)少—個(gè)參數(shù)(左操作數(shù))
- 不需要依賴友元就可以完成操作符重載
- 編譯器優(yōu)先在成員函數(shù)中尋找操作符重載函數(shù)

下面來(lái)實(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é)果如下:

這個(gè)說(shuō)明編譯器優(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++ 的強(qiáng)大特性之一
- 操作符重載的本質(zhì)是通過(guò)函數(shù)擴(kuò)展操作符的功能
- operator 關(guān)鍵字是實(shí)現(xiàn)操作符重載的關(guān)鍵
- 操作符重載遵循相同的函數(shù)重載規(guī)則
- 全局函數(shù)和成員函數(shù)都可以實(shí)現(xiàn)對(duì)操作符的重載
到此這篇關(guān)于C++超詳細(xì)講解操作符的重載的文章就介紹到這了,更多相關(guān)C++操作符重載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++超詳細(xì)分析講解內(nèi)聯(lián)函數(shù)
為了消除函數(shù)調(diào)用的時(shí)空開(kāi)銷,C++ 提供一種提高效率的方法,即在編譯時(shí)將函數(shù)調(diào)用處用函數(shù)體替換,類似于C語(yǔ)言中的宏展開(kāi)。這種在函數(shù)調(diào)用處直接嵌入函數(shù)體的函數(shù)稱為內(nèi)聯(lián)函數(shù)(Inline Function),又稱內(nèi)嵌函數(shù)或者內(nèi)置函數(shù)2022-06-06
C++語(yǔ)言基礎(chǔ) this和static關(guān)鍵字
這篇文章主要介紹了C++語(yǔ)言基礎(chǔ) this和static關(guān)鍵字,需要的朋友可以參考下2020-01-01
win10+VS2017+Cuda10.0環(huán)境配置詳解
這篇文章主要介紹了win10+VS2017+Cuda10.0環(huán)境配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
C++實(shí)現(xiàn)LeetCode(93.復(fù)原IP地址)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(93.復(fù)原IP地址),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
深入解析C++編程中對(duì)設(shè)計(jì)模式中的策略模式的運(yùn)用
這篇文章主要介紹了C++編程中對(duì)設(shè)計(jì)模式中的策略模式的運(yùn)用,需要的朋友可以參考下2016-03-03
C++基礎(chǔ)入門教程(九):函數(shù)指針之回調(diào)
這篇文章主要介紹了C++基礎(chǔ)入門教程(九):函數(shù)指針之回調(diào),本文講解了函數(shù)的地址、聲明函數(shù)指針、歷史原因、typedef挽救復(fù)雜的函數(shù)指針等內(nèi)容,需要的朋友可以參考下2014-11-11

