假设我有一个围绕原始 C++ 类型的自定义包装器:
class myInt
{
public:
/*stuff here*/
private:
unsigned int data;
}
我该怎么做
myInt mi;
if(mi){...}
以您期望的普通整数的方式表现?
我能想到的就是写一个显式的 boolean 类型转换,或者重写 operator==。
像这样的东西:
if(bool(mi)){...}
if(mi.toBool()){...}
if(mi==true){..}
所有这些确实有用,但看起来很丑陋和做作。
我还缺少其他选项吗?
最佳答案
最好的选择是完全放弃包装器。
关于c++ - 将自定义整数包装器转换为 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20893518/