網(wǎng)站首頁 編程語言 正文
運(yùn)算符重載
- 為什么要使用運(yùn)算符重載
-C/C++的運(yùn)算符,支持的數(shù)據(jù)類型,僅限于基本數(shù)據(jù)類型。
- 問題:一頭牛+一頭馬 = ?(牛馬神獸?)
一個(gè)圓 +一個(gè)圓 = ? (想要變成一個(gè)更大的圓)
一頭牛 – 一只羊 = ? (想要變成4只羊,原始的以物易物:1頭牛價(jià)值5只羊)
- 解決方案:
使用運(yùn)算符重載
方式一, 使用成員函數(shù)重載運(yùn)算符 需求:把牛肉換豬肉, 羊肉換豬肉
規(guī)則:
一斤牛肉:2斤豬肉
一斤羊肉:3斤豬肉
實(shí)現(xiàn):
牛 + 牛 = ?豬肉
牛 + 羊 = ?豬肉
?Cow類
> Cow.h #pragma once class Pork; class Sheep; class Cow{ //牛類 public: Cow(int weight = 0); //使用運(yùn)算符重載, 實(shí)現(xiàn) 牛肉 + 牛肉 = 豬肉 Pork operator+(const Cow& cow); //使用運(yùn)算符重載, 實(shí)現(xiàn) 牛肉 + 羊肉 = 豬肉 Pork operator+(const Sheep& sheep); private: int weight; //重量 }; _________________________________________________________________________________________________________________________________ > Cow.cpp #include "Cow.h" #include "Pork.h" #include "Sheep.h" Cow::Cow(int weight){ this->weight = weight; } //一斤牛肉換兩斤豬肉 Pork Cow::operator+(const Cow& cow){ return Pork((this->weight + cow.weight) * 2); } //一斤牛肉換兩斤豬肉, 一斤羊肉換三斤豬肉 Pork Cow::operator+(const Sheep& sheep){ int tmp = (this->weight * 2) + (sheep.getWeight() * 3); return Pork(tmp); }
?Sheep類
> Sheep.h #pragma once //羊類 class Sheep{ public: Sheep(int weight = 0); int getWeight() const; private: int weight; //重量 }; _________________________________________________________________________________________________________________________________ > Sheep.cpp #include "Sheep.h" Sheep::Sheep(int weight){ this->weight = weight; } int Sheep::getWeight() const{ return weight; }
Pork類
> Pork.h #pragma once #includeusing namespace std; class Pork{ //豬肉類 public: Pork(int weight = 0); string description() const; private: int weight; }; _________________________________________________________________________________________________________________________________ > Pork.cpp #include #include "Pork.h" Pork::Pork(int weight){ this->weight = weight; } string Pork::description() const{ stringstream ret; ret << this->weight << "斤"; return ret.str(); }
main.cpp
#include#include #include "Cow.h" #include "Pork.h" #include "Sheep.h" using namespace std; int main(void) { Pork p1; Cow c1(100); Cow c2(200); Sheep s1(100); //調(diào)用運(yùn)算符重載 Pork operator+(const Cow& cow); p1 = c1 + c2; cout << "牛 + 牛 = 豬肉:" << p1.description() << endl; //調(diào)用運(yùn)算符重載 Pork operator+(const Sheep& c1); p1 = c1 + s1; cout << "牛 + 羊 = 豬肉:" << p1.description() << endl; //羊+牛會(huì)報(bào)錯(cuò), 因?yàn)闆]有定義對應(yīng)的羊+牛運(yùn)算符重載 //p1 = s1 + c1; system("pause"); return 0; }
方式二, 使用非成員函數(shù)【友元函數(shù)】重載運(yùn)算符
實(shí)現(xiàn):
牛 + 牛 = ?豬肉
牛 + 羊 = ?豬肉
?Cow類
> Cow.h #pragma once class Pork; class Sheep; class Cow{ //牛類 public: Cow(int weight = 0); //使用友元運(yùn)算符重載, 實(shí)現(xiàn) 牛肉 + 牛肉 = 豬肉 friend Pork operator+(const Cow& c1, const Cow& c2); //使用友元運(yùn)算符重載, 實(shí)現(xiàn) 牛肉 + 羊肉 = 豬肉 friend Pork operator+(const Cow& c1, const Sheep& s1); private: int weight; //重量 }; _________________________________________________________________________________________________________________________________ > Cow.cpp #include "Cow.h" Cow::Cow(int weight){ this->weight = weight; }
?Sheep類
> Sheep.h #pragma once //羊類 class Sheep{ public: Sheep(int weight = 0); int getWeight() const; private: int weight; //重量 }; _________________________________________________________________________________________________________________________________ > Sheep.cpp #include "Sheep.h" Sheep::Sheep(int weight){ this->weight = weight; } int Sheep::getWeight() const{ return weight; }
Pork類
> Pork.h #pragma once #includeusing namespace std; class Pork{ //豬肉類 public: Pork(int weight = 0); string description() const; private: int weight; }; _________________________________________________________________________________________________________________________________ > Pork.cpp #include #include "Pork.h" Pork::Pork(int weight){ this->weight = weight; } string Pork::description() const{ stringstream ret; ret << this->weight << "斤"; return ret.str(); }
main.cpp
#include#include #include "Cow.h" #include "Pork.h" #include "Sheep.h" using namespace std; //要想訪問類的私有數(shù)據(jù)成員, 就把這個(gè)函數(shù)定義為友元 Pork operator+(const Cow& c1, const Cow& c2) { return ((c1.weight + c2.weight) * 2); } //要想訪問類的私有數(shù)據(jù)成員, 就把這個(gè)函數(shù)定義為友元 Pork operator+(const Cow& c1, const Sheep& s1) { return((c1.weight * 2) + (s1.getWeight() * 3)); } int main(void) { Pork p1; Cow c1(100); //100斤的牛 Cow c2(200); //200斤的牛 Sheep s1(100); //100斤的羊 //調(diào)用 friend Pork operator+(const Cow& c1, const Cow& c2); p1 = c1 + c2; cout << "使用友元 牛 + 牛 = 豬肉:" << p1.description() << endl; //調(diào)用 friend Pork operator+(const Cow& c1, const Sheep& s1); p1 = c1 + s1; cout << "使用友元 牛 + 羊 = 豬肉:" << p1.description() << endl; system("pause"); return 0; }
兩種方式的區(qū)別
區(qū)別:
使用成員函數(shù)來實(shí)現(xiàn)運(yùn)算符重載時(shí),少寫一個(gè)參數(shù),因?yàn)榈谝粋€(gè)參數(shù)就是this指針。
兩種方式的選擇:
- 一般情況下,單目運(yùn)算符重載,使用成員函數(shù)進(jìn)行重載更方便(不用寫參數(shù))
- 一般情況下,雙目運(yùn)算符重載,使用友元函數(shù)更直觀
方便實(shí)現(xiàn)a+b和b+a相同的效果,成員函數(shù)方式無法實(shí)現(xiàn)。
例如: 100 + cow; 只能通過友元函數(shù)來實(shí)現(xiàn)
cow +100; 友元函數(shù)和成員函數(shù)都可以實(shí)現(xiàn)
- 特殊情況:
(1)= () [ ] -> 不能重載為類的友元函數(shù)!!!(否則可能和C++的其他規(guī)則矛盾),只能使用成員函數(shù)形式進(jìn)行重載。
(2)如果運(yùn)算符的第一個(gè)操作數(shù)要求使用隱式類型轉(zhuǎn)換,則必須為友元函數(shù)(成員函數(shù)方式的第一個(gè)參數(shù)是this指針)
注意:
同一個(gè)運(yùn)算符重載, 不能同時(shí)使用兩種方式來重載,會(huì)導(dǎo)致編譯器不知道選擇哪一個(gè)(二義性)
總結(jié)
原文鏈接:https://blog.csdn.net/qq_34606496/article/details/123037789
相關(guān)推薦
- 2024-07-15 arthas操作spring被代理目標(biāo)對象命令速查
- 2022-12-03 pytorch模型保存與加載中的一些問題實(shí)戰(zhàn)記錄_python
- 2022-03-28 C++讀取wav文件中的PCM數(shù)據(jù)_C 語言
- 2022-05-21 云原生技術(shù)持久化存儲(chǔ)PV與PVC_云其它
- 2023-07-10 VMware三種網(wǎng)絡(luò)模式配置詳解。
- 2022-12-06 Pycharm配置anaconda環(huán)境圖文教程_python
- 2022-04-28 SpringBoot?整合mongoDB并自定義連接池的示例代碼_MongoDB
- 2022-11-13 C語言在輸入輸出時(shí)遇到的常見問題總結(jié)_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支