当前位置:网站首页>前置后置运算符重载
前置后置运算符重载
2022-08-09 14:59:00 【李昊19961128】
#include<iostream>
using namespace std;
//重载递增运算符
//自定义整型
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符 返回引用为了一直对一个数据进行递增操作
MyInteger& operator++()
{
//先进行++运算
m_Num++;
//再将自身做返回
return *this;
}
//重载后置++运算符
//void operator++(int) int代表占位参数,可以用于区分前置和后置递增
MyInteger operator++(int)
{
//先 记录当时结果
MyInteger temp = *this;
//后 递增
m_Num++;
//最后将记录结果做返回
return temp;
}
private:
int m_Num;
};
//重载<<运算符
ostream& operator<<(ostream& cout, MyInteger myint)
{
cout << myint.m_Num;
return cout;
}
void test01()
{
MyInteger myint;
cout << ++(++myint) << endl;
cout << myint << endl;
}
void test02()
{
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main() {
//test01();
test02();
//int a = 0;
//cout << ++(++a) << endl;
//cout << a << endl;
system("pause");
return 0;
}
边栏推荐
猜你喜欢
随机推荐
云模型和Logistic回归——MATLAB在数学建模中的应用(第2版)
NiN(Network in Network) pytorch实现
CTF在线加解密以及常用工具
hugging face tutorial - Chinese translation - sharing custom model
【The sword refers to Offer II 091. Paint the house】
【知识分享】知识链路-Modbus通信知识链路
CRM定制开发需要多少钱 CRM系统定制开发价格
【深度学习】原始问题和对偶问题(六)
Time series analysis
深入浅出最优化(4) 拟牛顿法
模糊综合评价
规划问题的MATLAB求解——MATLAB在数学建模中的应用(第2版)
"Deep learning" evaluation index of target detection
堆(heap)系列_0x04:堆的内部结构(_HEAP=_HEAP_SEGMENT+_HEAP_ENTRY)
Vim practical skills_4. Manage multiple files (open + split + save + netrw)
Heap series _0x02: The past and present of the heap (WinDbg+Visual Studio compilation)
Visio画神经网络卷积层
将类指针强制转换为void*指针进行传参的使用方法
交叉编译 OpenSSL
【力扣】128. 最长连续序列









