当前位置:网站首页>前置后置运算符重载
前置后置运算符重载
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;
}
边栏推荐
猜你喜欢
随机推荐
堆(heap)系列_0x0A:3种方法一次性解决堆溢出问题
【力扣】593. 有效的正方形
Hold face (hugging face) tutorial - Chinese translation - create a custom framework
AlexNet pytorch实现
主成分分析——MATLAB在数学建模中的应用(第2版)
[Deep Learning] SVM solves the linear inseparable situation (8)
Vim实用技巧_0.vim - introduction
ResNet 残差网络 一些粗略的理解
时间序列分析
PatchEmbed代码讲解记录
【力扣】98. 验证二叉搜索树
R-CNN Fast R-CNN Faster R-CNN总结
【力扣】207. 课程表
数学规划模型
【Postgraduate Work Weekly】
Visio画神经网络卷积层
蓝桥杯嵌入式备赛
SQLMap常用命令介绍
Heap series _0x03: heap block + malloc/new bottom layer + LFH (WinDbg analysis)
【深度学习】介绍六大类损失函数(九)