Access Modifiers
C++/CLI and IDisposable

C++/CLI: Instantiating Managed Objects

C++/CLI (not Beta 1) allows declaring reference types as local variables. Let's start with this simple ref class:

ref class Test
{
public:
    void Foo()
    {
        Console::WriteLine("Foo");
    }
};

Handles and the gcnew operator can be used to create instances:
Test^ t = gcnew Test();
t->Foo();

And without using a handle - this looks like a value type (this is new with Visual Studio 2005 versions after Beta 1):
Test t;
t.Foo();

What's the difference with the generated IL code? Nothing! Using a reference type as a local variable creates an object on the heap the same way as the version with the local variable. A reference type is not put on the stack.
A special advantage using local variables is with classes that implement IDisposable as you can read in my next weblog entry.

Christian

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Christian Nagel's OneNotes

AUTHOR: Christian Nagel's OneNotes
IP: 000.000.000.000
DATE: 01/15/2005 10:36:00 AM

Christian Nagel's OneNotes

AUTHOR: Christian Nagel's OneNotes
IP: 000.000.000.000
DATE: 01/15/2005 10:38:00 AM

coach outlet online

I agree you the up.As it give me the same some feeling!

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)