C++中运算符重载怎么用
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章主要介绍C++中运算符重载怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!运算符重载为什么要使用运算符重载-C/C++的运算符,支持的数据类型,仅限于基本数据类型。
千家信息网最后更新 2026年10月31日C++中运算符重载怎么用
这篇文章主要介绍C++中运算符重载怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
运算符重载
为什么要使用运算符重载
-C/C++的运算符,支持的数据类型,仅限于基本数据类型。
问题:一头牛+一头马 = ?(牛马神兽?)
一个圆 +一个圆 = ? (想要变成一个更大的圆)
一头牛 – 一只羊 = ? (想要变成4只羊,原始的以物易物:1头牛价值5只羊)
解决方案:
使用运算符重载
方式一, 使用成员函数重载运算符 需求:把牛肉换猪肉, 羊肉换猪肉
规则:
一斤牛肉:2斤猪肉
一斤羊肉:3斤猪肉
实现:
牛 + 牛 = ?猪肉
牛 + 羊 = ?猪肉
Cow类
> Cow.h#pragma onceclass Pork;class Sheep; class Cow{ //牛类public: Cow(int weight = 0); //使用运算符重载, 实现 牛肉 + 牛肉 = 猪肉 Pork operator+(const Cow& cow); //使用运算符重载, 实现 牛肉 + 羊肉 = 猪肉 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); //调用运算符重载 Pork operator+(const Cow& cow); p1 = c1 + c2; cout << "牛 + 牛 = 猪肉:" << p1.description() << endl; //调用运算符重载 Pork operator+(const Sheep& c1); p1 = c1 + s1; cout << "牛 + 羊 = 猪肉:" << p1.description() << endl; //羊+牛会报错, 因为没有定义对应的羊+牛运算符重载 //p1 = s1 + c1; system("pause"); return 0;}
方式二, 使用非成员函数【友元函数】重载运算符
实现:
牛 + 牛 = ?猪肉
牛 + 羊 = ?猪肉
Cow类
> Cow.h#pragma onceclass Pork;class Sheep; class Cow{ //牛类public: Cow(int weight = 0); //使用友元运算符重载, 实现 牛肉 + 牛肉 = 猪肉 friend Pork operator+(const Cow& c1, const Cow& c2); //使用友元运算符重载, 实现 牛肉 + 羊肉 = 猪肉 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;//要想访问类的私有数据成员, 就把这个函数定义为友元Pork operator+(const Cow& c1, const Cow& c2) { return ((c1.weight + c2.weight) * 2);}//要想访问类的私有数据成员, 就把这个函数定义为友元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斤的羊 //调用 friend Pork operator+(const Cow& c1, const Cow& c2); p1 = c1 + c2; cout << "使用友元 牛 + 牛 = 猪肉:" << p1.description() << endl; //调用 friend Pork operator+(const Cow& c1, const Sheep& s1); p1 = c1 + s1; cout << "使用友元 牛 + 羊 = 猪肉:" << p1.description() << endl; system("pause"); return 0;}
两种方式的区别
区别:
使用成员函数来实现运算符重载时,少写一个参数,因为第一个参数就是this指针。
两种方式的选择:
一般情况下,单目运算符重载,使用成员函数进行重载更方便(不用写参数)
一般情况下,双目运算符重载,使用友元函数更直观
方便实现a+b和b+a相同的效果,成员函数方式无法实现。
例如: 100 + cow; 只能通过友元函数来实现
cow +100; 友元函数和成员函数都可以实现
特殊情况:
(1)= () [ ] -> 不能重载为类的友元函数!!!(否则可能和C++的其他规则矛盾),只能使用成员函数形式进行重载。
(2)如果运算符的第一个操作数要求使用隐式类型转换,则必须为友元函数(成员函数方式的第一个参数是this指针)
注意:
同一个运算符重载, 不能同时使用两种方式来重载,会导致编译器不知道选择哪一个(二义性)
以上是"C++中运算符重载怎么用"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
猪肉
运算符
运算
函数
成员
牛肉
方式
羊肉
参数
数据
重量
C++
头牛
情况
类型
算符
中运
价值
内容
指针
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
金融科技还是互联网
奇酷互联网络科技
南京欣网互联网科技
腾讯科技 互联网女皇
北京禾田雨橡互联网科技
友谊互联网络科技
亿人帮互联网科技
天速互联网络科技有限公司
湖南诚度互联网科技有限公司
崇明区互联网教育科技诚信经营
互联网媒体平台算高科技产业
翼展科技 首家互联网医院
阿里互联网科技企业
锡山区互联网智能科技产品供应
互联网科技机房
广州智享互联网科技
互联网金融运营模式中的金融科技
米泉互联网科技专业怎么样
科技美学互联网 艺术
中国主要互联网科技公司
鸠鸠互联网科技
互联网与科技发展
卓越科技(互联网 )
融势互联网科技
无锡溥仪互联网科技有限公司
济南和联互联网科技有限公司
金融科技与互联网金融相同和不同
互联网金融科技元年
互联网公司属于科技吗
优雇互联网科技有限公司