Strange operator overloading

Hi,

I've a class that defines this:

operator bool () const { return (p != NULL) ; }

where p is a pointer to an object.

Which operator is overloaded in this construct maybe the operator()?

Tnx,
Daniele.
The conversion-to-bool operator.
Oh, I never used it! :-P

Now suppose that I have something like this:

1
2
3
4
5
6
7
8
class C
{
  private:
    FILE* p;
  public:
   C() { p = new FILE(.....); }
   operator bool () const { return (p != NULL) ; }
};


And I wanna do something like this:

1
2
3
4
5
6
7
C* c;
c = new C;

if (c)
  cout << "file loaded";
else
  cout << "file not loaded";


Of course this code doesn't work as expected because c is a pointer to C and "if(c)" only checks if c is valid.
So, How can invoke the conversion-to-bool in this case?

Tnx.
Daniele.
if (*c)
Last edited on
Simple, doesn't it? :-P

Thanks helios!
Daniele.
Topic archived. No new replies allowed.