condition op. overload for class

hi
can you overload class operators so that you can do this?

if(Smartpointer)
{}

and this:

if(Smartpointer == x)
{}

proplem i'm running to is that if you overload type conversion to bool then == operator doesn't get called.

thank you
Yes, you can. The first operator is

explicit Smartpointer::operator bool() const;

The second operator is

bool operator ==( const Smartpointer &, const Smartpointer & );


Last edited on
i forgot to mention that my c++ version (using visualstudio 2010) does not support explicit on type conversions...

tried and it gave this error:

error C2071: 'SafePtr<T>::operator bool' : illegal storage class


1
2
3
4
explicit  operator bool() const
 {
	return  p != 0;
 }


thanks
The second way is to write conversion operator to const void * without using explicit.
that did the job! :)

thanks
Topic archived. No new replies allowed.