I'm building a C++ library using POCO C++, and I'm using AutoPtr / SharedPtr to handle pointers. But I have a doubt about the proper use of these classes.
Scenario: A class holding a vector of pointers to objects implementing reference counting. How should I declare this vector?
- std::vector<MyRefCountingClass *> myVector;
- std::vector <AutoPtr<MyRefCountingClass>> myVector;
If I choose the first option, I can have a problem if:
- A user creates a new MyRefCountingClass instance using AutoPtr
- That user stores the created reference in my vector (so, I'm assigning an AutoPtr to a normal pointer).
- The user gets that element from the vector, and store the result in a different AutoPtr (now, the first AutoPtr and this one both claim the ownership of the object)
If in the step 3 I explicitly tell AutoPtr that it has to share ownership of the object (sharing = true or assign method of AutoPtr), I'd solve the problem, but it doesn't sound elegant to me. For that reason, I'd go with option 2: declaring my vector as holding AutoPtr instead of normal pointers.
Am I right with this approach?
Many thanks in advance