0

I have created a smart pointer class like:

template <class T>
class Owner
{
    T* m_p;
public:
    Owner(T *p=0) : m_p(p) {}
    ~Owner() { if (m_p) delete m_p; }
    T* operator ->() { return m_p; }
    T& operator *() { return *m_p; }
    // other members.
};

It works well (resembles to the auto_ptr in boost library), but now I have the requirements that I want to store an dynamic array of smart pointers in an obj, and it must support:

1) insert new smart pointer into the smart pointers array to let the array resize and acquire the ownership of the new obj,

2) delete one smart pointer on the fly and the resource get freed,

3) when finalizing the array, all objects get deleted.

I was thinking using std::vector<Owner<T> >, but seems c++ best practice suggests not storing smart pointers in std containers, because copy/assignment behaviors, so what other things can I employ to implement this? something like the OwnerArr in the below example:

 class Adapter;

 class Computer
 { 
 public:
      Computer() {}
      ~Computer() { // adapters get freed automatically here. }

      void insertAdapter(Adapter* pAdapter) { m_adapters->appendOne(pAdapter); }
      OwnerArr<Adapter>   m_adapters;
 };

Thanks in advance!

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Zhi Wang
  • 1,138
  • 4
  • 20
  • 34
  • 3
    Why not use the smart pointers that already exists? See e.g. [`std::shared_ptr`](http://en.cppreference.com/w/cpp/memory/shared_ptr) and [`std::unique_ptr`](http://en.cppreference.com/w/cpp/memory/unique_ptr). – Some programmer dude Sep 13 '12 at 10:23
  • 1
    `auto_ptr` is in the standard library, not boost. You want `shared_ptr` (or `unique_ptr` if you can use C++11.) – kennytm Sep 13 '12 at 10:24
  • 2
    You need copy operator and copy assignment operator ([the rule of three](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming))). You must figure out appropriate ownership transfer policy. Aside from learning, there is no reason to re-invent the wheel. – Remus Rusanu Sep 13 '12 at 10:24
  • 2
    Unless "other members" include a copy ctor and a a copy assignment operator, you're violating the rule of three. I totally agree with Joachim, Kenny and Remus here. How is your smart pointer going to be different from unique_ptr or shared_ptr? – sellibitze Sep 13 '12 at 10:35
  • Thanks for all of your comments! Yes, I can of course use auto_ptr or other standard smart pointer, my question here is about is there exists the smart pointer array tools? – Zhi Wang Sep 13 '12 at 10:39
  • @ZhiWang and the answer, as mentioned here before is **There is: `std::unique_ptr`**. – R. Martinho Fernandes Sep 13 '12 at 10:48

1 Answers1

0

You can't store your Owner, or std::auto_ptr (which you shouldn't use anyway, since it's deprecated), in a standard container because they are not really copyable.

In C++11, there's std::unique_ptr: a single-ownership smart pointer to replace auto_ptr, which is movable but not copyable. This can be moved or emplaced into any container, as long as you don't need to do anything that involves copying it. If you need multiple pointers to share ownership of the same object, then you can use std::shared_ptr instead.

If you're stuck with an older language version for some reason, then Boost can give you smart pointers, including a shared_ptr very similar to the standard one, or pointer containers similar to your OwnerArray.

If you do decide to roll your own resource management classes, always remember the Rule of Three. The classes you show have implicitly generated copy constructors and copy-assignment operators which can cause two pointers to own - and try to delete - the same object, which is very bad. You need to either prevent copying (by deleting these functions, or (pre-2011) declaring them private with no implementation), or implement some kind of safe copy semantics.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644