473,376 Members | 1,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 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 1550
"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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.