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ù)
故上述代碼可以直接寫(xiě)成:
#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語(yǔ)言實(shí)現(xiàn)統(tǒng)計(jì)100以內(nèi)所有素?cái)?shù)的個(gè)數(shù)
本文詳細(xì)講解了C語(yǔ)言實(shí)現(xiàn)統(tǒng)計(jì)100以內(nèi)所有素?cái)?shù)個(gè)數(shù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。需要的朋友可以收藏下,方便下次瀏覽觀看2021-11-11c++將字符串轉(zhuǎn)數(shù)字的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于c++將字符串轉(zhuǎn)數(shù)字的實(shí)例方法,有需要的朋友們可以參考下。2020-02-02C++?二叉樹(shù)的實(shí)現(xiàn)超詳細(xì)解析
二叉樹(shù)可以簡(jiǎn)單理解為對(duì)于一個(gè)節(jié)點(diǎn)來(lái)說(shuō),最多擁有一個(gè)上級(jí)節(jié)點(diǎn),同時(shí)最多具備左右兩個(gè)下級(jí)節(jié)點(diǎn)的數(shù)據(jù)結(jié)構(gòu)。本文將詳細(xì)介紹一下C++中二叉樹(shù)的實(shí)現(xiàn)和遍歷,需要的可以參考一下2022-03-03C語(yǔ)言實(shí)現(xiàn)獲取內(nèi)存信息并輸出的實(shí)例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)獲取內(nèi)存信息并輸出的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03C語(yǔ)言變長(zhǎng)數(shù)組 struct中char data[0]的用法詳解
下面小編就為大家?guī)?lái)一篇C語(yǔ)言變長(zhǎng)數(shù)組 struct中char data[0]的用法詳解。小編覺(jué)得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01詳解c語(yǔ)言實(shí)現(xiàn)的內(nèi)存池(適用于兩個(gè)線程、不加鎖、效率高)
這篇文章主要介紹了c語(yǔ)言實(shí)現(xiàn)的內(nèi)存池(適用于兩個(gè)線程、不加鎖、效率高),設(shè)計(jì)一個(gè)內(nèi)存池,要求效率比系統(tǒng)調(diào)用的效率要高(測(cè)試1萬(wàn)次),同時(shí)支持一個(gè)線程申請(qǐng),另外一個(gè)線程釋放,需要的朋友可以參考下2024-02-02C++?基礎(chǔ)函數(shù)的介紹及使用(Vector+deque+STL)
這篇文章主要介紹了C++?基礎(chǔ)函數(shù)的介紹及使用(Vector+deque+STL),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06