473,549 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Testing for a null managed pointer

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
Jan 9 '07 #1
7 4863
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
br************* *********@hotma il.com
Remove only "_nos_pam"
Jan 9 '07 #2
Excelent thanks alot I thought that it must be something like that failed to
find that in the documentation when i looked.

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
br************* *********@hotma il.com
Remove only "_nos_pam"

Jan 9 '07 #3
Robin Imrie wrote:
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

Jan 9 '07 #4
"Carl Daniel [VC++ MVP]" wrote:
FYI, You can use nullptr for native pointers as well.
So, is NULL redefined to nullptr somewhere in VC++ headers?

--PA

Jan 10 '07 #5

"Pavel A." <pa*****@NOwrit emeNO.comwrote in message
news:76******** *************** ***********@mic rosoft.com...
"Carl Daniel [VC++ MVP]" wrote:
>FYI, You can use nullptr for native pointers as well.

So, is NULL redefined to nullptr somewhere in VC++ headers?
No, nullptr evaluates to 0 (same as NULL) when used as a native pointer.
>
--PA

Jan 10 '07 #6
Ben Voigt wrote:
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
Jan 10 '07 #7

"Tamas Demjen" <td*****@yahoo. comwrote in message
news:eD******** ********@TK2MSF TNGP02.phx.gbl. ..
Ben Voigt wrote:
>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:
Agreed.

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

#define NULL nullptr
Jan 10 '07 #8

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

Similar topics

1
3593
by: SSRoy | last post by:
I cant use the NULL constant to check a pointer (eg. say Object*) whether it is valid or not - when I use C++ .NET. What comes in place of NULL in the managed extension of C++ Pls Help. Sincerely Sanjoy Sinharoy
6
4658
by: Andrej Prsa | last post by:
Hi! Is there a way to reliably test whether a string was ever declared? E.g. char *str; test (str); /* -> no */ str = strdup ("contents"); test (str); /* -> yes */
102
5935
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ?...
19
6770
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like what happens in memory operations, everyone knows the importance of freeing the allocated memory, but there do have memory leaks from time to
7
1854
by: Lev | last post by:
Hi, I have an unmanaged pointer to a class that I want to hold in a managed class. I pass the pointer (from unmanaged code) in the constructor of the managed class, and at that point it has value. It is stored in a member variable in my managed class declared as MyObj __nogc* pMyObj. When I try to retrieve it later, using a function...
4
5711
by: William F. Kinsley | last post by:
My understanding is that when I re-compile a existing MFC application with the /clr switch, that the code generated is managed(with some exceptions) but that the data isn't, i.e. not garbage collected. I also noticed that when replaced some of the existing MFC dialogs with managed winforms that everything is still running in the same app...
27
4195
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
3
4692
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting...
30
2430
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just carrying the original pointer. In other words, for the simplest usage code: * no overhead (just carrying a pointer or two), and * no...
0
7520
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.