其他分享
首页 > 其他分享> > 递增运算符重载

递增运算符重载

作者:互联网

前置递增返回引用,后置递增返回值;

后置递增不能实现链式编程,c++内置就是这样;

后置递增占位符(int),从而与前置递增区分;

(不确定)递增运算符重载只能定义在类内,且格式较固定 

#include<iostream>
#include<string>
using namespace std;

class MyInteger
{
public:
	MyInteger()
	{
		m_A = 0;
	}
	int m_A;
	//后置递增运算符,不能够实现链式编程;
	MyInteger operator++(int)
	{
		MyInteger temp;
		temp = *this;
		m_A++;
		return temp;
	}
	MyInteger& operator++()
	{
		m_A++;
		return *this;
	}
};

//重载左移运算符;
ostream& operator<<(ostream& cout, MyInteger m)
{
	cout << m.m_A << endl;
	return cout;
}


int main() 
{
	MyInteger m1;
	
	//int a=0;cout << (a++)++ << endl;
	cout << m1++ << endl;
	cout << m1 << endl;
	cout << ++(++m1) << endl;
	cout << m1 << endl;
	system("pause"); 
	return 0;
}

标签:MyInteger,后置,++,递增,运算符,int,重载
来源: https://blog.csdn.net/weixin_46432495/article/details/121799087