0

在我的课堂上,我有成员函数:

const bool operator&&(const KinematicVariable &right) const { 
        return this->isUsed() && right.isUsed(); 
}
inline const bool isUsed() const { return this->_used; }

然后我尝试

if (k1 && k2 && k3)

但我明白了

error: C2677: binary '&&' : no global operator found which takes type 
'KinematicVariable' (or there is no acceptable conversion)
4

1 回答 1

5

首先,k1 && k2将被评估为一个布尔值,然后你将拥有that_bool && k3,你不提供operator&&for 的重载(也不应该!)。看来您真正想做的根本不是超载任何东西:

if (k1.isUsed() && k2.isUsed() && k3.isUsed())

或者,您可以提供显式转换bool为以下成员KinematicVariable

explicit operator bool() const { return isUsed(); }

要在 C++03 中执行此操作,请使用safe-bool idiom

于 2013-03-03T17:36:53.460 回答