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++ / March 2007

Tip: Looking for answers? Try searching our database.

Return ref struct

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Maxim - 19 Mar 2007 22:46 GMT
Hi all,

Why is the following code doesn't work?

public ref struct MyType
{
.................
};

MyType GetMyType()
{
MyType result;
.....................
return result;
}

The compiler says

Error 3 error C2440: 'return' : cannot convert from 'MyLib::MyType' to
'MyLib::MyType'

Thanks in advance
Maxim - 19 Mar 2007 22:59 GMT
After changing this to

public ref struct MyType
{
................
};

MyType ^ GetMyType()
{
MyType result;
....................
return %result;
}

This code compiles without errors.

But to the best of my knowledge this involves boxing procedure. Is it
possible to avoid it in this case?

> Hi all,
>
[quoted text clipped - 18 lines]
>
> Thanks in advance
Ben Voigt - 20 Mar 2007 14:13 GMT
> After changing this to
>
[quoted text clipped - 14 lines]
> But to the best of my knowledge this involves boxing procedure. Is it
> possible to avoid it in this case?

It's a reference type, so no boxing is performed.  The local variable isn't
actually on the stack, it has stack semantics (your Dispose method, if any,
will be called when the function exits even though you return the value,
which is a different flavor of the native C++ problem if you try to return a
reference to a non-static local variable).

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.