472,960 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 software developers and data experts.

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 1534
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gandalf | last post by:
Hello. I have some questions about the standard containers. How does the standard containers behave if I do queue<Foo> myQ; queue<Foo> myQ2; .... insert into myQ... myQ = myQ2;
6
by: Mark | last post by:
If you have STL containers (like list, vector,...) in functions as automatic variables, do the nodes that are put on the containers get buffered so they can be reused by later container...
14
by: phil_gg04 | last post by:
Dear C++ Experts, Over the last couple of months I have been writing my first program using shared memory. It has been something of an "in-at-the-deep-end" experience, to say the least. At...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
5
by: Matthias Kaeppler | last post by:
Hi, I was wondering, since STL containers are based around copying, whether it's a good idea to use reference counted smart pointers, such as boost::shared_ptr in STL containers. I can't store...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
19
by: AlesD | last post by:
Hello, I have problem that when I use std::list<MyClassand then store various subclasses of MyClass in that list (or any other STL container) the instances get sliced. I have read FAQ: ' What...
35
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.