Re: Testing for nullptr for a ref object
- From: "Bruno van Dooren" <bruno_nos_pam_van_dooren@xxxxxxxxxxx>
- Date: Sun, 23 Apr 2006 17:52:57 +0200
Do you say the answer is trivial? If so I must admit I disagree, but thenFor a ref object x, can I say "if (x)" rather than "if (x != nullptr)"
to test that x is pointing to something ?
Yes, you can.
a quick test would have given you the answer immediatly.
I'm must admit I haven't read much of the C++/CLI standard.
Anyway, my interpretation is that the compiler will consider a conversion
from x to bool better than the check against nullptr.
So if a conversion from x to bool exists it will be chosen over the
!= nullptr check (BTW: there is no standard conversion from
handle-to-bool as there is for native pointers).
Hi Holger,
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.
there was a discussion about this topic 1 or 2 weeks ago.
my preference is to only use 'if' with real bools that are either return
values or the result of an equation.
my point was that if you want to know for sure, just do a quick test.
of course it could be dangerous to rely on the automatic handle to bool
conversion
because if soemone adds a conversion to bool later in the development
traject, your code suddenly starts doing funny things.
I admit I didn't think of the case where there was a conversion to bool.
Thanks for correcting me.
--
Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@xxxxxxxxxxx
Remove only "_nos_pam"
.
- Follow-Ups:
- Re: Testing for nullptr for a ref object
- From: Holger Grund
- Re: Testing for nullptr for a ref object
- References:
- Testing for nullptr for a ref object
- From: Edward Diener
- Re: Testing for nullptr for a ref object
- From: Bruno van Dooren
- Re: Testing for nullptr for a ref object
- From: Holger Grund
- Testing for nullptr for a ref object
- Prev by Date: Re: Testing for nullptr for a ref object
- Next by Date: Re: VS 2005 C++ and sphelper.h - SAPI
- Previous by thread: Re: Testing for nullptr for a ref object
- Next by thread: Re: Testing for nullptr for a ref object
- Index(es):
Relevant Pages
|