Reputation: 95
I read that for example
MyClass<T> ^ mc = new MyClass<T>();
gives a handle of mc.
I also read, that using ^mc will create the object in the cli-heap that will automatically cleaned up, so I don't have to use
delete mc
then.
Wouldn't that be an argument to only use ^-handles ?
Upvotes: 0
Views: 86
Reputation: 49
^
is a managed pointer. It is CLI equivalent of a smart pointer.
I also read, that using
^mc
will create the object in the cli-heap that will automatically cleaned up, so I don't have to usedelete mc
then.
Yes, that's true. CLI runtime has a garbage collector. But CLI is a Windows specific thing so it won't work in other environments.
Upvotes: 0
Reputation: 5095
^
pointers are only used in the Microsoft Common Language Runtime modification of of the language. If you use them, you are locking your project to use only the Microsoft compiler. If you're looking for portability, you should use traditional pointers that must be manually deleted or using the new C++11 features.
Upvotes: 2
Reputation: 76240
This is an extension, but you can achieve something similar in standard C++ using:
auto mc = std::make_unique<MyClass<T>>();
And yes, that's a good reason to use std::unique_ptr
and/or std::shared_ptr
whenever you can.
Upvotes: 1