#includeusing namespace std; // 2021年10月12日 08:52:38 晴 // 今天学习运算符重载,一开始是懵逼,然后是惊叹,可是学到单目运算符重载时,愣住了 // 然后一种失落感涌上心头,情不自禁,失声痛哭, // C++怎么可以如此玩弄i++,++i,还有天理吗,还有王法吗,我学不下去,太毁三观了 // C++是一门邪恶的语言,虚拟世界的东西真是一点都不能信 class Int { public: Int(int i = 0); ~Int(); void Show(); Int operator ++(); Int operator ++(int); private: int i; }; Int::Int(int i) { this->i = i; } void Int::Show() { cout << "=" << i << endl; } Int Int::operator ++()//单目运算符前置 { Int temp; this->i = this->i + 1; temp.i = this->i; return temp; } Int Int::operator ++(int)//单目运算符后置 { Int temp; temp.i = this->i; this->i = this->i + 1; return temp;//返回一个Int对象,给别的Int赋值 } Int::~Int() { } int main() { Int a(0), b, c(0), d; //b = a++; b = a.operator++(1000);//i++:单目运算符后置 //d = ++c; d = c.operator++();//++i:单目运算符前置 cout << "a"; a.Show(); cout << "b"; b.Show(); cout << "c"; c.Show(); cout << "d"; d.Show(); return 0; }



