Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2007

Tip: Looking for answers? Try searching our database.

*long vs Int32 tracking reference

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rob@boltman.org - 26 Jan 2007 11:07 GMT
Hi,

I'm a bit confused by the mapping of the native types int and long to
UInt32. I'm trying to bind a tracking reference to a native value type
and it works for Int32 % -> int but not for Int32 % -> long.

This page http://msdn2.microsoft.com/en-gb/library/0wf2yk2k(VS.80).aspx
states that both int & long map to Int32.

The following code prints "Hello World 1" rather than the expected
"Hello World 32". If I change the 2 long types to ints (as commented)
the code works as expected, printing "Hello World 32".

using namespace System;

void fUniversalArg(Int32 %var)
{
    var = 32;
}

void fNativeArg(long *pVar  /* int *pVar */)
{
    fUniversalArg((Int32)*pVar);
}

int main(array<System::String ^> ^args)
{
//    int var = 1;
    long var = 1;
    fNativeArg(&var);

   Console::WriteLine(L"Hello World " + var);
   return 0;
}

Any thoughts - I guess I am mising something...

Cheers
Rob
Tamas Demjen - 26 Jan 2007 20:13 GMT
> This page http://msdn2.microsoft.com/en-gb/library/0wf2yk2k(VS.80).aspx
> states that both int & long map to Int32.

That's correct, but it just means that long and int are both 32-bit
signed integers under Win32 and Win64, and they're both compatible with
System::Int32. But that doesn't mean logn and int are the same exact
type. long is not simply a typedef for int. Even in native code, they're
not interchangeable types:

void f(int& value);
void test()
{
   long value;
   f(value); // error or warning message
}

My C++ reports an error "cannot convert parameter 1 from 'long' to
'int&'. My Borland compiler remports a warning and creates a temporary
object, whose value gets discarded after the function call.

> void fNativeArg(long *pVar  /* int *pVar */)
> {
>     fUniversalArg((Int32)*pVar);

Here the compiler creates a silent temporary object. fNativeArg modifies
your temporary, which gets discarded right away. Your code is
essentially equivalent with this:
void fNativeArg(long *pVar  /* int *pVar */)
{
   Int32 value = *pVar;
   fUniversalArg(value);
}

Note how pVar will never get updated.

What you want is this:

void fNativeArg(long *pVar  /* int *pVar */)
{
   fUniversalArg((Int32%)*pVar);
}

After adding the % symbol, your program will print 32.

Tom
Rob - 27 Jan 2007 10:43 GMT
> But that doesn't mean logn and int are the same exact
> type. long is not simply a typedef for int. Even in native code, they're
> not interchangeable types:

Yes, I understood that...

> Here the compiler creates a silent temporary object. fNativeArg modifies
> your temporary, which gets discarded right away. Your code is
> essentially equivalent with this:

... but I hadn't thought through to what the compiler was generating.

> What you want is this:
>
[quoted text clipped - 3 lines]
>
> }After adding the % symbol, your program will print 32.

Thanks! I knew I was probably doing something daft.

Cheers
Rob

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.