#include <iostream>
using namespace std;
/* 运算符重载 -- 双目运算符 a+b, a+=b, a==b, a<b...
* 格式:
* 1 全局函数或静态成员函数: return_type operator symbol(param1,param2
* 2 类成员: return_type operator symbol(param1),另一个参数是类的属性
*/
class Base {
private:
unsigned int numBase;
public:
/*operator a+b 的实现*/
Base operator + (int input) {
Base newBase(numBase + input);
return newBase;
}
/*operator a+=b 的实现*/
Base& operator += (int input) {
numBase += input;
return *this; //this指当前对象的地址,*this指当前对象
}
/*operator a+=b 的实现*/
bool operator == (Base& input) {
if (numBase == input.numBase)
return true;
return false;
第三十八节 C++ 运算符重载 - 双目运算符
最新推荐文章于 2023-06-14 09:36:50 发布
本文通过实例介绍了C++中如何进行运算符重载,特别是双目运算符如`+`,`+=`和`==`的使用。通过创建对象并执行这些运算,展示了运算符重载在类中的应用,从而实现自定义行为。
摘要由CSDN通过智能技术生成