best practices using a list of auto_ptr's?

TruthHunter
TruthHunter used Ask the Experts™
on
Hi,

I have a question about "best practices" on how to implement a list of auto_ptr's that I'm hoping someone can help me with.

I have several classes derived from the same abstract base class, and I would like to keep pointers to the classes in some sort of container to easily traverse and invoke an inherited method within each.

The problem is I had wanted to wrap the pointers to the classes in auto_ptr's, but then I read a convincing article about why auto_ptr's should not be used in std containers:

http://www.gotw.ca/publications/using_auto_ptr_effectively.htm

So I'm left scratching my head and wondering what's the (best) solution?  Max points available!  Here's a quasi pseudo-code description of what I'm trying to do (this may not compile, it's just presented quickly to try to make the question clearer).

Thanks for any help!

class A
{
  
  public:
    A();
    ~A();
    virtual void F() = 0;
}

class B : public A
{
  public:
    B();
    ~B();
    virtual void F();
}

class C : public A
{
  public:
    C();
    ~C();
    virtual void F();
}

class D : public A
{
  public:
    D();
    ~D();
    virtual void F();
}

// Idea 1 - shouldn't do because no auto_ptr's in std
//          containers.
vector< auto_ptr< A > > v;
v.push_back( new B() );
v.push_back( new C() );
v.push_back( new D() );

for( int i = 0; i < v.size(); i++ )
{
  (v.at(i))->F();
}

// Idea 2 - simple array but isn't initializer behavior
//          unpredictable, if one of the new()'s fails?
auto_ptr< A > arr[3] = { new B(), new C(), new D() };

for( int j = 0; j < 3; j++ )
{
  arr[i]->F();
}

// Idea 3?

// Idea 4?

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer (Avast)
Commented:
You can't use an auto_ptr in a standard container, it's really that simple. The standard doesn't actually allow it since the result of doing so is undefined. Instead you need to use a reference counted smart pointer, and I recommend boost::shared_ptr

http://www.boost.org/doc/libs/1_44_0/libs/smart_ptr/shared_ptr.htm
evilrixSenior Software Engineer (Avast)
Commented:
If you are interested, I also show how you can develop your own reference counted smart pointer in my article on smart pointers.
http://www.experts-exchange.com/Programming/Languages/CPP/A_1959-C-Smart-pointers.html
Top Expert 2009
Commented:
>> why auto_ptr's should not be used in std containers:

I'm sure that's the reason the article gives too, but you should not use auto_ptr's in containers (even if the compiler lets you), because copying auto_ptr's do not have the right behavior when copying them.

You could either store normal pointers, or store a different kind of smart pointer that CAN be used in a container, like a shared_ptr.
Or you could create a container that properly cleans up after itself - you might call it a smart container.
Top Expert 2009

Commented:
oh my ... slow morning lol

Author

Commented:
Thank you guys!  If I can just grab the boost shared_ptr on its own I will (the app's not that large), or else I will try evilrix's home-grown class.  Thanks again for the help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial