How do I override the bool operator in a C++ class?

后端 未结 4 1309
遇见更好的自我
遇见更好的自我 2020-12-09 10:31

I\'m defining a ReturnValue class in C++ that needs to report whether a method was successful. I want objects of the class to evaluate to true on s

4条回答
  •  盖世英雄少女心
    2020-12-09 11:14

    The simple answer is providing operator bool() const, but you might want to look into the safe bool idiom, where instead of converting to bool (which might in turn be implicitly converted to other integral types) you convert to a different type (pointer to a member function of a private type) that will not accept those conversions.

提交回复
热议问题