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語言實現(xiàn)統(tǒng)計100以內(nèi)所有素數(shù)的個數(shù)
本文詳細講解了C語言實現(xiàn)統(tǒng)計100以內(nèi)所有素數(shù)個數(shù)的方法,文中通過示例代碼介紹的非常詳細。需要的朋友可以收藏下,方便下次瀏覽觀看2021-11-11C語言變長數(shù)組 struct中char data[0]的用法詳解
下面小編就為大家?guī)硪黄狢語言變長數(shù)組 struct中char data[0]的用法詳解。小編覺得挺不錯的現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01詳解c語言實現(xiàn)的內(nèi)存池(適用于兩個線程、不加鎖、效率高)
這篇文章主要介紹了c語言實現(xiàn)的內(nèi)存池(適用于兩個線程、不加鎖、效率高),設(shè)計一個內(nèi)存池,要求效率比系統(tǒng)調(diào)用的效率要高(測試1萬次),同時支持一個線程申請,另外一個線程釋放,需要的朋友可以參考下2024-02-02C++?基礎(chǔ)函數(shù)的介紹及使用(Vector+deque+STL)
這篇文章主要介紹了C++?基礎(chǔ)函數(shù)的介紹及使用(Vector+deque+STL),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06