Testing for a null managed pointer

  • Thread starter Thread starter Robin Imrie
  • Start date Start date
R

Robin Imrie

Hi,

I have some code like this....

MyObject ^o = gcnew MyObject();

My2ndObject ^obj2 = o->GetObject();

if( obj2 != NULL )
{
//... do stuff
}

when i compile this I get an error(C2446) no conversion from 'int' to
'My2ndObject ^'
no user-defined-conversion operator available, or
no standard conversion exists from the boxed form of the arithetic type
to the target type

I could do this in VS2003 what do I now need to do now for VS2005?

Thanks

Robin
 
I have some code like this....
MyObject ^o = gcnew MyObject();

My2ndObject ^obj2 = o->GetObject();

if( obj2 != NULL )
{
//... do stuff
}

when i compile this I get an error(C2446) no conversion from 'int' to
'My2ndObject ^'
no user-defined-conversion operator available, or
no standard conversion exists from the boxed form of the arithetic type
to the target type

I could do this in VS2003 what do I now need to do now for VS2005?

Hi,
NULL is an pointer sized variable that happens to have a decimal value of 0.
For managed pointers you can use nullptr. Syntax highlighting will color it
blue.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Excelent thanks alot I thought that it must be something like that failed to
find that in the documentation when i looked.

Robin
 
Robin said:
Excelent thanks alot I thought that it must be something like that
failed to find that in the documentation when i looked.

FYI, You can use nullptr for native pointers as well.

-cd
 
Ben said:
No, nullptr evaluates to 0 (same as NULL) when used as a native pointer.

nullptr has a very big advantage to NULL, though. The expression nullptr
implicates that the value is a pointer, while NULL doesn't. Here's an
exmaple:

void f(void*)
{
std::cout << "f(void*)\n";
}

void f(int)
{
std::cout << "f(int)\n";
}

int main()
{
f(NULL);
f(nullptr);
}

This prints
f(int)
f(void*)

NULL was recognized as an integer (its value is the integer 0
constant), while nullptr is treated as a real pointer (as if it was
(void*)0).

Currently the keyword doesn't work in native projects. It must be a /CLR
project in VC8. However, nullptr is very likely becoming a C++ standard
in 2009. Over time, more and more compilers will support it.

Tom
 
Tamas Demjen said:
nullptr has a very big advantage to NULL, though. The expression nullptr
implicates that the value is a pointer, while NULL doesn't. Here's an
exmaple:

Agreed.

But I was responding to Pavel, who hearing that nullptr worked for native
pointers, wondered whether the SDK headers did

#define NULL nullptr
 
Back
Top