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!

The comments to this entry are closed.