>> Your example also works with native C++. if you create a handle class
that has a bool conversion, you can
do something like
if(myHandle)
{
}
to check if the handle in your object is valid or not.
I'm afraid I don't quite understand. What do you mean with "native c++"
and "handle class"? Obviously, standard C++ doesn't have handles.
The language designers often claim that handles are very similar to
pointers.
Hi Holger,
I think was not clear enough about the meaning of 'handle' in the context of
my example.
Someone had a question about a self developed class that he used for
wrapping a file handle. (hence the term 'handle class').
His question was if and how he could enable his class to be used like this:
if(myHandle)
{
}
to represent the fact that myHandle contained a valid file handle or not.
providing a conversion to bool solves this problem.
this will of course only work for instances. not pointers. that is why I
suggested providing an explicit IsValid method for his class.
in the case of the op, suppose you have a ref class on which you use if(x)
to check for a nullptr.
if someone adds a conversion to bool sometime after you have programmed your
part, your code will suddenly start behaving erroneously.
I agree with you that relying on implicit conversion is not a good idea
because it makes code harder to read and maintain.
especially if you didn't write it yourself.
--
Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"