Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C++/CLI smart pointer project (e.g. scoped_ptr)?

Is there a C++/CLI RAII smart pointer class for containment of a native pointer in a managed type? Just wondering, before I go write my own clr_scoped_ptr value class template.

I'm aware of the Microsoft-provided:

  • containment of a managed handle in a native class: auto_gcroot

  • containment of a managed handle in a managed class: auto_handle

The above two are similar to auto_ptr or unique_ptr.

  • I gave skeleton code for a counted_handle here, similar to shared_ptr

But all these are for disposing managed ref class instances, not for freeing native objects.

like image 773
Ben Voigt Avatar asked Mar 28 '11 02:03

Ben Voigt


People also ask

Are there smart pointers in C?

C++11 comes up with its own mechanism that's Smart Pointer. When the object is destroyed it frees the memory as well. So, we don't need to delete it as Smart Pointer does will handle it. A Smart Pointer is a wrapper class over a pointer with an operator like * and -> overloaded.

What is Scoped_ptr C++?

The scoped_ptr class template stores a pointer to a dynamically allocated object. (Dynamically allocated objects are allocated with the C++ new expression.) The object pointed to is guaranteed to be deleted, either on destruction of the scoped_ptr, or via an explicit reset.

What is smart pointer in C ++ 11?

Concept of the C++11 Smart Pointers Smart pointers are class objects that behave like built-in pointers but also manage objects that you create with new so that you don't have to worry about when and whether to delete them - the smart pointers automatically delete the managed object for you at the appropriate time.


1 Answers

This one looks fairly complete, but I'm not looking for silent transfer of ownership ala auto_ptr.


I've posted my version under a rather permissive license over at codereview.se

like image 125
Ben Voigt Avatar answered Sep 28 '22 09:09

Ben Voigt