转换操作符
是一种特殊的类成员函数。将类类型值转变为其他类型值的转换。
operator type()
必须是成员函数,不能指定返回类型,并且形参表必须为空
例如:
class Test {
public:
Test(int t){ t_ = t; }
~Test(){}
//operator int(){ return t_; }
operator void*(){ return this; }
private:
int t_;
};
用法:
int main(int argc, char *argv[])
{
Test test(1);
if (test) {
printf("test ok!/n");
}
system("PAUSE");
return EXIT_SUCCESS;
}