473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Testing for nullptr for a ref object

For a ref object x, can I say "if (x)" rather than "if (x != nullptr)"
to test that x is pointing to something ?
Apr 23 '06 #1
5 2703
> For 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.

Note that this check does not indicate if you already disposed(deleted) x or
not.
delete x;
if(x)
{
//will be true after the delete
}

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 23 '06 #2
"Bruno van Dooren" <br**********************@hotmail.com> wrote
For 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.

Do you say the answer is trivial? If so I must admit I disagree, but then
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).

And indeed this is what VC++ does. The program below prints

x
!y

for me.

using System::Console;
ref class X {};
ref struct Y { operator bool() { return false; } };
int main()
{
X^ x = gcnew X;
Y^ y = gcnew Y;
if (x) Console::WriteLine("x"); else Console::WriteLine("!x");
if (y) Console::WriteLine("y"); else Console::WriteLine("!y");
}

-hg
Apr 23 '06 #3
>>> For 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.

Do you say the answer is trivial? If so I must admit I disagree, but then
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
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 23 '06 #4
Hi Bruno,

"Bruno van Dooren" <br**********************@hotmail.com> wrote
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.

If you used the example with native classes (i.e.struct/class instead
of ref struct/ref class) and pointers instead of handles (explicitly
initialized oc.),
you'd get different results, because the compiler doesn't consider
user-defined
operators on pointer types (and even if C++ allowed that the
pointer-to-bool
conversion is a standard conversion and therefore would have a better
conversion rank anyway).
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.

I was more thinking of the lines of function or class template which works
for most template arguments, just not for the one you didn't test it with
....

But then, at least for C++ developers it should be clear that an implicit
conversion is rarely a good idea (especially since, bool-to-int-promotion
is a standard conversion, too and therefore a UDC to bool followed
by integral promotion to int is a valid conversion sequence.

-hg
Apr 23 '06 #5
>> 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"
Apr 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is...
15
by: Enrique | last post by:
Question I am posting this question again (3rd time) because some issues with my no spam alias. Here it is the question: I have not been able to run unit tests for a VSTO (2005) project. I...
1
by: bonk | last post by:
I have a native class that has a managed private member: private: gcroot<MyManagedRefType^> m_managed; Now at some point in time this member will eventually get initialized: m_managed =...
1
by: Vinod | last post by:
Hi, In VC8 project, I am having a struct which is having a char* variable. Now I am creating a 3 elements array object for the struct. I send the base address of the object using VARIANT to a...
11
by: Tony Maresca | last post by:
I'm having a hard time understanding why I get the following error with the code shown. Can someone explain? public ref class Class1 { }; generic<typename Twhere T: Class1, gcnew()
0
by: --== Alain ==-- | last post by:
Hi, I would like to define the default value of my property. My property is from type Bitmap^, therefore i was thinking to do : but i does not work. how is it possible to define this...
0
by: craig.conboy | last post by:
Using MC++ .Net 2.0, with the CLR Profiler (from Microsoft) I am seeing that 3 blocks of memory, each ~48MB in size, being allocated by some code that initializes a managed array. It is a jagged...
7
by: Robin Imrie | last post by:
Hi, I have some code like this.... MyObject ^o = gcnew MyObject(); My2ndObject ^obj2 = o->GetObject(); if( obj2 != NULL ) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.