[C++基础]022_运算符重载
1 #include <iostream> 2 using namespace std; 3 4 class Tree{ 5 public: 6 Tree(){ 7 num = 0; 8 } 9 void operator ++(){ 10 num++; 11 } 12 13 int num; 14 }; 15 16 int main(){ 17 18 Tree tree; 19 cout<<tree.num<<endl; 20 tree++; 21 cout<<tree.num<<endl; 22 23 system("pause"); 24 return 0; 25 }
上述代码重载了++运算符之后,就可以直接用对象进行++运算了,打印结果如期,分别是0和1