【C++深度剖析教程8】C++的操作符重载的概念
之前學(xué)習(xí)了類的函數(shù)重載的概念,今天學(xué)習(xí)操作符重載的概念。在這之前我們先看一個(gè)例子:
上面是一個(gè)復(fù)數(shù)的加法,a為復(fù)數(shù)的實(shí)部,b為復(fù)數(shù)的虛部,在main函數(shù)里我想實(shí)現(xiàn)復(fù)數(shù)c1與c2的加法。很顯然,正常的+號(hào)操作符是不能實(shí)現(xiàn)復(fù)數(shù)的相加減的。那么我們可以添加功能函數(shù)實(shí)現(xiàn)讓兩個(gè)復(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 + c2printf("c3.a = %d,c3.b = %d\n",c3.getA(),c3.getB());return 0; }打印如下:
c3.a = 4,c3.b = 6說明程序?qū)崿F(xiàn)了兩個(gè)復(fù)數(shù)相加減的功能。
上面的Add函數(shù)解決了Complex對象的相加減的問題,但是Complex是現(xiàn)實(shí)世界中確實(shí)存在的復(fù)數(shù),并且復(fù)數(shù)在數(shù)學(xué)中的地位與普通的實(shí)數(shù)相同。
為什么不能讓 + 號(hào)這個(gè)操作符也支持復(fù)數(shù)相加呢?
由此我們你就想到了一個(gè)概念,操作符重載的概念:
- C++中的重載能夠擴(kuò)展到操作符的功能
- 操作符的重載以函數(shù)的方式進(jìn)行
本質(zhì):
- 用特殊形式的函數(shù)擴(kuò)展操作符的功能
操作符重載的用法:
- 通過operator關(guān)鍵字可以定義特殊函數(shù)
- operator的本質(zhì)是通過函數(shù)重載操作符
語法:
Type operator Sign(const Type& p1, const Type& p2) {Type ret;return ret; }其中Sign為系統(tǒng)中預(yù)定義的操作符,如:+,- ,* ,/,等
將上面的程序改寫為:
運(yùn)行結(jié)果為:
c3.a = 4,c3.b = 6這說明,我們的修改是對的,我們已經(jīng)解決了這個(gè)復(fù)數(shù)相加的問題。
但是我們上述的程序都用到了友元的概念,但是友元一般不會(huì)出現(xiàn)在現(xiàn)代軟件工程中。所以我們還可以改進(jìn):
- 可以將操作符重載函數(shù)定義為類的成員函數(shù)
- 比全局操作符重載函數(shù)少一個(gè)參數(shù)(左操作數(shù))
- 不需要依賴友元就可以實(shí)現(xiàn)操作符重載
- 編譯器優(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;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; }運(yùn)行結(jié)果為:
Complex operator +(const Complex& p) c3.a = 4,c3.b = 6分析運(yùn)行結(jié)果以及程序:
運(yùn)行結(jié)果打印的Complex operator +(const Complex& p),說明編譯器優(yōu)先在成員函數(shù)中尋找操作符重載函數(shù),而沒有選擇全局變量中的重載函數(shù)。計(jì)算結(jié)果正常,說明可以將操作符重載函數(shù)定義為類的成員函數(shù)。
程序中類的成員重載函數(shù)Complex operator +(const Complex& p),只有一個(gè)參數(shù),少一個(gè)參數(shù)。并且c1 + c2 等同于:c1.operator + (c2)。
總結(jié):
- 操作符重載是C++強(qiáng)大的特性之一
- 操作符重載的本質(zhì)是通過函數(shù)擴(kuò)展操作符的功能
- operator關(guān)鍵字是實(shí)現(xiàn)操作符的關(guān)鍵
- 操作符重載遵循相同的函數(shù)重載法則
- 全局函數(shù)和成員函數(shù)都可以實(shí)現(xiàn)對操作符的重載
想獲得各種學(xué)習(xí)資源以及交流學(xué)習(xí)的加我(有我博客中寫的代碼的原稿):
qq:1126137994
微信:liu1126137994
可以共同交流關(guān)于嵌入式,操作系統(tǒng),C++語言,C語言,數(shù)據(jù)結(jié)構(gòu)等技術(shù)問題。
總結(jié)
以上是生活随笔為你收集整理的【C++深度剖析教程8】C++的操作符重载的概念的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Qt正则表达式提取全路径的文件名
- 下一篇: s3c2440移植MQTT