#include <iostream> using namespace std; class Test { private: int num; public: Test():num(0) {} Test& operator=(const int &num) { this->num = num; return *this; } Test& operator=(const Test &test) { this->num = test.num; return *this; } Test operator++(int)//后置 { Test temp = *this; ++(this->num); return temp; } Test& operator++()//前置,返回的是引用 { ++(this->num); return *this; } operator int()//向int转换 { return num; } }; int main() { Test t1,t2; int nt1 = ++t1; cout<<"应该是1,实际是"<<nt1<<endl;//输出1 int nt2 = t2++; cout<<"应该是0,实际是"<<nt2<<endl;//输出0 return 0; }
时间: 2024-10-17 15:04:47