Is it possible to have a class object return a true/false value, so I can do something like this:
MyClass a;
...
if (a)
do_something();
I can accomplish (almost) what I want by overloading the ! operator:
class MyClass {
...
bool operator!() const { return !some_condition; };
...
}
main()
MyClass a;
...
if (!a)
do_something_different();
but I haven’t found a way to overload what would be the “empty” operator. Of course, using the == operator to check for true/false is also possible, and is in fact what I have been doing so far.
Overload the void * cast operator:
this is how streams work, allowing you to say:
The use of void * rather than bool prevents the cast being used by mistake in some contexts, such as arithmetic, where it would not be desirable. Unless, you want to use it in those contexts, of course.