containers and pointers

V

Vincenzo Cappello

I need a map that contain different object of the same base class like:

std::map< int, base_class* >

someone tell me is no correct using pointer in containers so i change to:

std::map< int, std::auto_ptr< base_class > >

someone tell me is no correct using auto_ptr in containers...

someone can tell me the right way?
 
J

John Carson

Vincenzo Cappello said:
I need a map that contain different object of the same base class
like:

std::map< int, base_class* >

someone tell me is no correct using pointer in containers so i change
to:

It is correct to use pointers in containers, but you then have to worry
about manual memory management (assuming that what is pointed to is
dynamically allocated).
std::map< int, std::auto_ptr< base_class > >

someone tell me is no correct using auto_ptr in containers...

They were right.
someone can tell me the right way?

If you want automatic memory management, then Boost shared_ptr or
shared_array is the usual solution:

http://www.boost.org/libs/smart_ptr/smart_ptr.htm
 
R

roberts.noah

John said:
They were right.

This is because auto_ptr is not copyable. If you copy or assign from
an auto_ptr to another the copyee changes its pointer to NULL. Only
one auto_ptr can have the same pointer at the same time. You can break
this semantic of course but standard containers don't and so can't use
auto_ptr.
 
D

Default User

Vincenzo said:
I need a map that contain different object of the same base class
like:

std::map< int, base_class* >

someone tell me is no correct using pointer in containers

"Someone" is incorrect.
so i change to:

std::map< int, std::auto_ptr< base_class > >

someone tell me is no correct using auto_ptr in containers...

Now, this is wrong.
someone can tell me the right way?

You either have to use a third-party smart pointer, or manage the
container. One way is to create a manager class that has the map as a
data member, then the destructor for that class can take care of
deleting all the contained pointers.



Brian
 
A

Axter

Vincenzo said:
I need a map that contain different object of the same base class like:

std::map< int, base_class* >

someone tell me is no correct using pointer in containers so i change to:

std::map< int, std::auto_ptr< base_class > >

someone tell me is no correct using auto_ptr in containers...

someone can tell me the right way?

Use a smart pointer that has value semantics for comparison operator.
The boost shared_ptr doesn't have value semantics, and IMHO, it's not
the right smart pointer for this task.
The following smart pointers do have value semantics, and moreover have
automatic cloning.
http://code.axter.com/cow_ptr.h
http://code.axter.com/copy_ptr.h
http://code.axter.com/smart_ptr.h

Example:
std::map< int, cow_ptr< base_class > >

Check out the following link for partial help documents on usage:
http://axter.com/smart_ptr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top