C++運(yùn)算符重載的詳細(xì)講解
加號(hào)運(yùn)算符重載
對(duì)于內(nèi)置數(shù)據(jù)類(lèi)型,編譯器知道如何運(yùn)算
但是對(duì)于自己封裝的類(lèi),編譯器無(wú)法進(jìn)行運(yùn)算
這時(shí)可以通過(guò)自己定義運(yùn)算符重載進(jìn)行運(yùn)算
operator+
通過(guò)成員函數(shù)重載+號(hào)
#include<iostream> using namespace std; class Person { public: int m_a; int m_b; //通過(guò)成員函數(shù)實(shí)現(xiàn)重載 Person operator+ (Person &p) { //創(chuàng)建一個(gè)臨時(shí)變量 Person temp; temp.m_a = this->m_a + p.m_a; temp.m_b = this->m_b + p.m_b; return temp; } }; void test01() { Person p1; p1.m_a = 66; p1.m_b = 44; Person p2; p2.m_a = 6; p2.m_b = 4; Person p3; //通過(guò)函數(shù)原型調(diào)用 p3 = p1.operator+(p2); //簡(jiǎn)便調(diào)用 //p3 = p1 + p2; cout << "p3.m_a:" << p3.m_a << endl; cout << "p3.m_b:" << p3.m_b << endl; } int main() { test01(); system("pause"); return 0; }
注意兩種調(diào)用方式
通過(guò)函數(shù)原型調(diào)用
p3 = p1.operator+(p2);
簡(jiǎn)便調(diào)用
p3 = p1 + p2;
通過(guò)全局函數(shù)重載+號(hào)
#include<iostream> using namespace std; class Person { public: int m_a; int m_b; }; //通過(guò)全局函數(shù)實(shí)現(xiàn)重載 Person operator+ (Person& p1, Person& p2) { //創(chuàng)建一個(gè)臨時(shí)變量 Person temp; temp.m_a = p1.m_a + p2.m_a; temp.m_b = p1.m_b + p2.m_b; return temp; } void test01() { Person p1; p1.m_a = 66; p1.m_b = 44; Person p2; p2.m_a = 6; p2.m_b = 4; Person p3; //函數(shù)原型調(diào)用 p3 = operator+(p1,p2); //簡(jiǎn)便調(diào)用 //p3 = p1 + p2; cout << "p3.m_a:" << p3.m_a << endl; cout << "p3.m_b:" << p3.m_b << endl; } int main() { test01(); system("pause"); return 0; }
注意兩種調(diào)用方式
通過(guò)函數(shù)原型調(diào)用
p3 = operator+(p1,p2);
簡(jiǎn)便調(diào)用
p3 = p1 + p2;
運(yùn)算符重載發(fā)生函數(shù)重載
運(yùn)算符重載可以發(fā)生函數(shù)重載:Person+int等等
#include<iostream> using namespace std; class Person { public: int m_a; int m_b; }; //通過(guò)全局函數(shù)實(shí)現(xiàn)重載 Person operator+ (Person& p1, int num) { //創(chuàng)建一個(gè)臨時(shí)變量 Person temp; temp.m_a = p1.m_a + num; temp.m_b = p1.m_b + num; return temp; } void test01() { Person p1; p1.m_a = 66; p1.m_b = 44; Person p2; p2.m_a = 6; p2.m_b = 4; Person p3; //函數(shù)原型調(diào)用 //p3 = operator+(p1,55); //簡(jiǎn)便調(diào)用 p3 = p1 + 55; cout << "p3.m_a:" << p3.m_a << endl; cout << "p3.m_b:" << p3.m_b << endl; } int main() { test01(); system("pause"); return 0; }
調(diào)用方法和定義方法與上面相同,不再多余贅述
總結(jié)
1、系統(tǒng)內(nèi)置數(shù)據(jù)類(lèi)型的表達(dá)式不可改變
2、不要濫用運(yùn)算符重載
左移運(yùn)算符
不利用成員函數(shù)重載左移運(yùn)算符
沒(méi)有具體演示,因?yàn)閳?bào)錯(cuò),我也沒(méi)寫(xiě)出來(lái)
下面通過(guò)全局函數(shù)實(shí)現(xiàn)
#include<iostream> using namespace std; class Person { public: int m_a; int m_b; }; ostream& operator<<(ostream& cout,Person&p) { cout << "p.m_a=" <<p. m_a << " p.m_b=" <<p. m_b << endl; return cout; } void test01() { Person p1; p1.m_a = 44; p1.m_b = 66; cout << p1 << endl; } int main() { test01(); system("pause"); return 0; }
因?yàn)橐獙?shí)現(xiàn)鏈?zhǔn)?,?shí)現(xiàn)追加,所以返回值必須是ostream
總結(jié)
配合友元實(shí)現(xiàn)自定義輸出類(lèi)型
遞增運(yùn)算符重載
遞增運(yùn)算符重載
#include<iostream> using namespace std; class myInt { friend ostream& operator<<(ostream& cout, myInt num); public: myInt() { this->m_a = 0; } //前置++運(yùn)算符重載 myInt& operator++()//返回引用是為了一直對(duì)一個(gè)數(shù)據(jù)進(jìn)行遞增,否則函數(shù)默認(rèn)返回一個(gè)新的數(shù) { //先進(jìn)行++ m_a++; //然后返回自身 return *this; } //后置++運(yùn)算符重載 myInt operator++(int)//int表示占位參數(shù),用于區(qū)分前置后置參數(shù) { //先記錄當(dāng)前的值 myInt temp=*this; //再遞增 m_a++; //然后返回記錄的值 return temp; } private: int m_a; }; //左移運(yùn)算符重載 ostream& operator<<(ostream& cout, myInt num) { cout << num.m_a; return cout; } void test01() { myInt myint; cout << myint << endl; cout << ++myint << endl; cout << myint << endl; } int main() { test01(); system("pause"); return 0; }
賦值運(yùn)算符重載
#include<iostream> using namespace std; class Person { public: Person(int num)//將數(shù)據(jù)開(kāi)辟到堆區(qū) { m_a = new int(num); } ~Person() { if (m_a != NULL) { delete m_a; m_a = NULL; } } //重載賦值運(yùn)算符 Person& operator=(Person &p)//返回值用Person返回本身,可執(zhí)行連等 { //先判斷是否有屬性在堆區(qū),如果有先釋放干凈 if (m_a != NULL) { delete m_a; m_a = NULL; } m_a = new int(*p.m_a); return *this; } int* m_a; }; void test01() { Person p1(18); Person p2(209); Person p3(9); p2 = p1 = p3; cout << *p1.m_a << endl; cout << *p2.m_a << endl; cout << *p3.m_a << endl; } int main() { test01(); system("pause"); return 0; }
關(guān)系運(yùn)算符重載
#include<iostream> using namespace std; #include<string> class Person { public: Person(string name, int age) { this->age = age; this->name = name; } bool operator==(Person& p) { if (this->age == p.age && this->name == p.name) { return true; } else { return false; } } bool operator!=(Person& p) { if (this->age == p.age && this->name == p.name) { return false; } else { return true; } } string name; int age; }; void test01() { Person p1("gouride", 19); Person p2("gouride", 19); if (p1 == p2) { cout << "p1和p2相同" << endl; } else { cout << "p1和p2不相同" << endl; } if (p1 != p2) { cout << "p1和p2不相同" << endl; } else { cout << "p1和p2相同" << endl; } } int main() { test01(); system("pause"); return 0; }
函數(shù)調(diào)用重載
仿函數(shù)
#include<iostream> using namespace std; #include<string> class Myprint { public: void operator()(string name) { cout << name << endl; } int operator()(int a,int b) { return a + b; } }; void test01() { Myprint myprint; myprint("測(cè)試"); //匿名對(duì)象調(diào)用 cout << Myprint()(4,6) << endl; } int main() { test01(); system("pause"); return 0; }
總結(jié)
到此這篇關(guān)于C++運(yùn)算符重載的文章就介紹到這了,更多相關(guān)C++運(yùn)算符重載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C 語(yǔ)言環(huán)境設(shè)置詳細(xì)講解
本文主要介紹C 語(yǔ)言環(huán)境設(shè)置,在不同的系統(tǒng)平臺(tái)上,C語(yǔ)言的環(huán)境設(shè)置不同,這里幫大家整理了Liunx, UNIX,Windows 上安裝C語(yǔ)言環(huán)境,有開(kāi)始學(xué)習(xí)C語(yǔ)言的朋友可以參考下2016-08-08C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05用C編寫(xiě)一個(gè)送給女朋友的情人節(jié)小程序 可愛(ài)!
非??蓯?ài)的情人節(jié)小程序!文章為大家分享了用C編寫(xiě)一個(gè)送給女朋友的小程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02C++模擬實(shí)現(xiàn)stack和Queue的操作示例
這篇文章主要介紹了C++模擬實(shí)現(xiàn)stack和Queue的操作示例,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06VSCode (Visual Studio Code) V1.43.0下載并設(shè)置成中文語(yǔ)言的方法
Visual Studio Code是一款免費(fèi)開(kāi)源的現(xiàn)代化輕量級(jí)代碼編輯器,支持語(yǔ)法高亮、智能代碼補(bǔ)全、自定義熱鍵、括號(hào)匹配、代碼片段、代碼對(duì)比 Diff、GIT 等特性,這篇文章主要介紹了VSCode (Visual Studio Code) V1.43.0下載并設(shè)置成中文語(yǔ)言,需要的朋友可以參考下2020-03-03C++ TensorflowLite模型驗(yàn)證的過(guò)程詳解
這篇文章給大家介紹了C++ TensorflowLite模型驗(yàn)證的過(guò)程,測(cè)試代碼,主要是RunInference()和read_file(),詳細(xì)操作過(guò)程跟隨小編一起看看吧2021-08-08C++?sqlite3數(shù)據(jù)庫(kù)配置使用教程
SQLite 是一種嵌入式的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),它是一個(gè)開(kāi)源項(xiàng)目,已經(jīng)被廣泛應(yīng)用于各種應(yīng)用程序和操作系統(tǒng)中,這篇文章主要介紹了C++?sqlite3數(shù)據(jù)庫(kù)配置使用,需要的朋友可以參考下2023-08-08