Search code examples
c++auto-ptr

how can I use auto_ptr as member variable that handles another member variable


I have a class like this:

class A 
{

 private:

 B* ptr;

}

But B ptr is shared among different A objects. How can I use auto_ptr so that when A gets destructed B stays on so that other A objects that point to the same ptr can continue without issues. Does this look ok:

class A
{
public:

 auto_ptr< B > m_Ptr;

private:

 B* ptr;

}

what are the different ways people have implemented this and any issues/advantages they saw one to another... Thanks


Solution

  • What you're looking for is shared_ptr. It handles exactly this type of scenario.

    This is a part of the BOOST library though and not STL so it may not be available on your particular platform. However if you google around a bit you can find a lot of standalone refcounted pointer implementations that will satisfy your needs here.