可能重复:
Overloading the global type conversion operator
Operator overloading
如何声明operator bool()(用于转换)而不是类方法?
Fox示例我有struct Car,我不想在这个struct方法中声明:operator bool()
。
我想要的是什么:
struct Car
{
float fuel;
};
//global operator
bool opreator bool(Car car)
{
return car.fuel > 0;
}
Car car1;
car1.fuel = 100;
bool b = car1;