470,810 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

containers and pointers

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?
Feb 15 '06 #1
4 1383
"Vincenzo Cappello" <vi******@localhost.localdomain> wrote in message
news:pa***************************@localhost.local domain
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
--
John Carson

Feb 15 '06 #2

John Carson wrote:
std::map< int, std::auto_ptr< base_class > >

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


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.

Feb 15 '06 #3
Vincenzo Cappello wrote:
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
Feb 15 '06 #4
Vincenzo Cappello wrote:
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

Feb 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gandalf | last post: by
6 posts views Thread by Mark | last post: by
14 posts views Thread by phil_gg04 | last post: by
18 posts views Thread by Matthias Kaeppler | last post: by
5 posts views Thread by Matthias Kaeppler | last post: by
19 posts views Thread by AlesD | last post: by
35 posts views Thread by dragoncoder | last post: by
15 posts views Thread by Nindi73 | last post: by
21 posts views Thread by George Exarchakos | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.