#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;
}