002: 看上去好坑的运算符重载
程序员文章站
2022-03-26 16:06:09
...
#include <iostream>
using namespace std;
class MyInt
{
int nVal;
public:
MyInt( int n) { nVal = n ;}
MyInt& operator-(int n)
{
nVal-=n;
return *this;
}
int getnVal() const
{
return nVal;
}
friend int Inc(const MyInt& my)
{
return my.getnVal()+1;
}
};
int Inc(int n) {
return n + 1;
}
int main () {
int n;
while(cin >>n) {
MyInt objInt(n);
objInt-2-1-3;
cout << Inc(objInt);
cout <<",";
objInt-2-1;
cout << Inc(objInt) << endl;
}
return 0;
}