Auto_ptr or shared_ptr equivalent in C ++ / CLI managed classes

In C ++ / CLI, you can use native types in a managed class, so you are not allowed to hold a member of the native class in a managed class: in this case, you need to use pointers.

Here is an example:

class NativeClass { .... }; public ref class ManagedClass { private: NativeClass mNativeClass; // Not allowed ! NativeClass * mNativeClass; // OK auto_ptr<NativeClass> mNativeClass; //Not allowed ! boost::shared_ptr<NativeClass> mNativeClass; //Not allowed ! }; 

Does anyone know about the equivalent of shared_ptr in the C ++ / CLI world?

Edit: Thank you for your offer of "1800-Information." Following your suggestion, I checked STL.Net, but it is only available with Visual Studio 2008, and it provides containers + algorithms, but without smart pointers.

+6
smart-pointers auto-ptr c ++ - cli
source share
3 answers

I found the answer to codeproject :

Nishant Sivakumar published an article about it at http://www.codeproject.com/KB/mcpp/CAutoNativePtr.aspx

On this page also find Denis Shevchenko's comment: it provides an stl-like implementation that works quite well.

+2
source share

I have not tested this completely, but what about the following:

 #pragma once #include <memory> template <class T> public ref class m_shared_ptr sealed { std::shared_ptr<T>* pPtr; public: m_shared_ptr() : pPtr(nullptr) {} m_shared_ptr(T* t) { pPtr = new std::shared_ptr<T>(t); } m_shared_ptr(std::shared_ptr<T> t) { pPtr = new std::shared_ptr<T>(t); } m_shared_ptr(const m_shared_ptr<T>% t) { pPtr = new std::shared_ptr<T>(*t.pPtr); } !m_shared_ptr() { delete pPtr; } ~m_shared_ptr() { delete pPtr; } operator std::shared_ptr<T>() { return *pPtr; } m_shared_ptr<T>% operator=(T* ptr) { pPtr = new std::shared_ptr<T>(ptr); return *this; } T* operator->() { return (*pPtr).get(); } }; 

This should allow you to use C ++ 11 / Boost shared_ptrs interchangebly in ref classes.

+1
source share

STL.Net is described here . I do not know what condition he is in or what he can use for you.

0
source share

All Articles