其他分享
首页 > 其他分享> > 015 看上去好坑的运算符重载

015 看上去好坑的运算符重载

作者:互联网

#include <iostream> 
using namespace std;
class MyInt 
{ 
	int nVal; 
	public: 
	MyInt( int n) { nVal = n ;}
MyInt& operator- (int n) {
        nVal -= n;
        return *this;
    }
    operator int() {
        return nVal;
    }
}; 
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;
}

标签:return,int,运算符,operator,MyInt,015,nVal,重载,objInt
来源: https://www.cnblogs.com/icefield817/p/15914035.html