Sneedlewoods
Sneedlewoods

Reputation: 95

C++ only using ^ instead of *?

I read that for example

MyClass<T> ^ mc = new MyClass<T>();

gives a handle of mc.

  1. Can I use ^mc the same way I use *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

Answers (3)

Geekus Maximus
Geekus Maximus

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 use delete 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

coladict
coladict

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

Shoe
Shoe

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

Related Questions