C++ 运算符重载 类型转换
1 #include <iostream> 2 using namespace std; 3 4 class Good 5 { 6 public: 7 int member; 8 Good(int a): member(a) 9 { 10 cout << "调用构造" << endl; 11 cout << "member =" << member << endl; 12 } 13 void show() 14 { 15 cout << member << endl; 16 } 17 18 operator int() 19 { 20 return member; 21 } 22 23 operator bool() 24 { 25 cout << "调用bool转换" << endl; 26 return (bool)member; 27 } 28 29 }; 30 31 int main(int argc, char *argv[]) 32 { 33 Good a(10), b(20); 34 cout << (bool)a << endl; 35 a.show(); 36 return 0; 37 }