I\'m trying to implement my own boolean class, but cannot replicate native semantics for &&. The following contrived code demonstrates the issue:
You should not overload bool operator&&
, since you lose short circuit evaluation, as you have discovered.
The correct approach would be to give your class a bool conversion operator
class MyBool {
public:
bool theValue;
MyBool() {}
MyBool(bool aBool) : theValue(aBool) {}
explicit operator bool() { return theValue; }
};
Note that explicit conversion operators require C++11 compliance. If you do not have this, have a look at the safe bool idiom.