473,394 Members | 1,567 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,394 software developers and data experts.

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 4855
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**********************@hotmail.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**********************@hotmail.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*****@NOwritemeNO.comwrote in message
news:76**********************************@microsof t.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****************@TK2MSFTNGP02.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
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. ...
6
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
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"...
19
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...
7
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...
4
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...
27
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
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...
30
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.