C++中的类型重载很好用,甚至可以基于类型重载发明一种新的编程语言。
下面简单写个demo来展示类型重载,如果要自己写个好用的类,还需要重载等号等运算符。
class CTest
{
public:
operator int()
{
return 1;
}
operator bool()
{
return false;
}
operator std::string()
{
return "ffff";
}
};
int main()
{
CTest test;
int i = test;
bool j = test;
std::string str1 = test;
std::cout<
CTest类的对象既可以当int、bool类型,甚至还可以当标准库中的字符串类型。



