cpp——复合类型——函数——运算符重载
来源:互联网 发布:网络医疗 编辑:程序博客网 时间:2024/06/05 05:31
运算符重载
c++引入运算符重载机制目的为了类类型(枚举类型)模仿内置类型运算符操作,因此运算符重载必须确保至少一个参数为类类型(枚举类型),且必须是对象形式,而非指针(引用)形式:
- 运算符重载机制不能改变运算符的操作数个数
- 运算符重载机制不能改变运算符的优先级
- 运算符重载机制不能改变现有类型(内置类型&复合类型)支持的运算符含义,如果重载两个整型的加运算符,改变两个整型的加运算符含义,则编译系统会混乱不堪
- 运算符重载本质还是函数,只是函数名特别的函数,函数名中包含operator和运算符,因此运算符重载函数返回值可为void,且可重载(运算符重载函数形参个数固定,只能通过形参类型不同重载)
enum运算符重载
enum EPoint5{ ZERO, ONE, TWO, THREE, FOUR,};EPoint5 operator+(EPoint5 point1, int point2){ int ret = ((int)point1 + point2) % 5; cout << "operator+(EPoint5, int) = " << ret << endl; switch(ret) { case 0: return ZERO; case 1: return ONE; case 2: return TWO; case 3: return THREE; case 4: return FOUR; } return ZERO;}EPoint5 operator+(int point1, EPoint5 point2){ int ret = (point1 + (int)point2) % 5; cout << "operator+(int, EPoint5) = " << ret << endl; switch(ret) { case 0: return ZERO; case 1: return ONE; case 2: return TWO; case 3: return THREE; case 4: return FOUR; } return ZERO;}EPoint5 operator+(EPoint5 point1, EPoint5 point2){ int ret = ((int)point1 + (int)point2) % 5; cout << "operator+(EPoint, EPoint5) = " << ret << endl; switch(ret) { case 0: return ZERO; case 1: return ONE; case 2: return TWO; case 3: return THREE; case 4: return FOUR; } return ZERO;}void enum_op_overload(){ EPoint5 point = THREE; point + 3 + 8; 3 + point + 8; point + point; operator+(point, 3); operator+(3, point); operator+(point, point);}
output:
operator+(EPoint5, int) = 1operator+(EPoint5, int) = 4operator+(int, EPoint5) = 1operator+(EPoint5, int) = 4operator+(EPoint, EPoint5) = 1operator+(EPoint5, int) = 1operator+(int, EPoint5) = 1operator+(EPoint, EPoint5) = 1
class运算符重载
class CPoint{public: CPoint(int point) : mPoint(point) {} public: int mPoint;};CPoint operator+(CPoint point1, int point2){ int ret = (point1.mPoint + point2) % 5; cout << "operator+(CPoint, int) = " << ret << endl; return CPoint(ret);}CPoint operator+(int point1, CPoint point2){ int ret = (point1 + point2.mPoint) % 5; cout << "operator+(int, CPoint) = " << ret << endl; return CPoint(ret);}CPoint operator+(CPoint point1, CPoint point2){ int ret = (point1.mPoint + point2.mPoint) % 5; cout << "operator+(CPoint, CPoint) = " << ret << endl; return CPoint(ret);}void class_op_overload(){ CPoint point(3); point + 3 + 8; 3 + point + 8; point + point; operator+(point, 3); operator+(3, point); operator+(point, point);}
output:
operator+(CPoint, int) = 1operator+(CPoint, int) = 4operator+(int, CPoint) = 1operator+(CPoint, int) = 4operator+(CPoint, CPoint) = 1operator+(CPoint, int) = 1operator+(int, CPoint) = 1operator+(CPoint, CPoint) = 1
总结
支持重载的运算符:
算术操作符:+,-,*,/,%
复合算术操作符:
位操作符
复合
0 0
- cpp——复合类型——函数——运算符重载
- cpp——复合类型——函数——函数重载
- CPP 6th——运算符重载+函数重载+函数模板
- cpp——复合类型——引用
- cpp——复合类型——函数——匿名形参 默认实参
- CPP类和对象—四个默认成员函数+运算符重载
- c——运算符——复合类型
- swift——运算符——复合类型
- 复合类型 —— 引用
- 复合类型 —— 指针
- 运算符重载(2)———流运算符重载函数
- 详谈面向对象的重载——>“函数重载”与“运算符重载”
- c——复合类型——函数
- C++ : 复数运算“<<”函数重载和“+”、“—”重载
- C++运算符重载——重载二元运算符
- 《剑指offer》——重载赋值运算符函数
- 运算符(+ /*)的重载——友元函数
- 复数类中的运算符重载—友元函数
- 斐波那契数列的编程
- 阿里巴巴2015算法工程师实习生笔试卷
- NOIP 2005 普及组 复赛 apple 淘淘摘苹果
- android 圆形虚线背景
- 建造器模式(builder)
- cpp——复合类型——函数——运算符重载
- TP框架实现发送邮件
- ios之NSCache的使用
- Java集合---ConcurrentHashMap原理分析
- 图像拼接(八):拼接多幅图像+Matlab实现+Stanford Open Course
- bootstrap-进度条
- Java面试整理 关于hibernate
- 为什么要使用工厂模式
- 内连接、左外连接、右外连接、交叉连接区别