C++基础(十五)运算符重载(a++ ++a ())

#include <iostream>
using namespace std;

class MyInteger
{
	friend ostream& operator<<(ostream& cout, MyInteger myint);
private:
	int m_Num;
public:
	MyInteger(int num)
	{
		this->m_Num = num;
	}
	MyInteger& operator++() 
	{
		this->m_Num++;
		return *this;
	}
	//后置++利用占位参数
	MyInteger operator++(int)
	{
		MyInteger temp = *this;
		this->m_Num++;
		return temp;
	}
};

ostream& operator<<(ostream &cout ,MyInteger myint) 
{
	cout << myint.m_Num;
	return cout;
}

int main()
{
	MyInteger myint(5);
	cout << ++(++myint) << endl;
	cout << myint++ << endl;
	cout << myint << endl;
	return 0;
}

运算符重载()

#include <iostream>
using namespace std;

class Add 
{
public:
	int operator()(int a,int b) 
	{
		return a + b;
	}
};

int main()
{
	//匿名对象
	cout << Add()(10, 60) << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/we1less/article/details/108891166