Reputation: 1815
It's possible to overload the not operator for a class:
class TestA
{
public:
bool Test;
const bool operator!(){return !Test;}
};
So that...
TestA A; A.Test = false;
if(!TestA){ //etc }
...can work. However, how does the following work?
if(TestA) //Does this have to be overloaded too, or is it the not operator inverted?
I will add, having read it, I am slightly confused by the typedef solution that is employed, and I don't fully understand what is occurring, as it seems somewhat obsfucated. Could someone break it down into an understandable format for me?
Upvotes: 2
Views: 267
Reputation: 4674
You could write an operator bool()
. This takes care of coercion to bool, making statements as your above one possible.
Upvotes: 4
Reputation: 96233
You overload operator void*
(like the standard library's iostreams) or use boost's trick of using an "unspecified_bool_type" typedef
(safe bool). It does NOT automatically invert your operator!
Upvotes: 2