How can I store derived classes in a map

T

Tom

I am trying to put together a system that involves 3 classes derived
form a common base class, which is working fine.

Except that I want to store them in a map and I find that I cannot do
this as I thought I could.

I have the following classes

class base_class{}
class a : base_class{int aa}
class b : base_class{char bb}
class c : base_class{double cc}

Then I create a map like this

typedef std::map<std::string,base_class, std::less<std::string> >
map_objects;
typedef map_of_objects::value_type map_of_objects_value;

I insert a object value to the map like this

a object = a();
objects.insert(map_of_objects_value("name", object));

Then I read it back like this

a object;
map_of_objects::iterator itor = objects.find("name");

if (itor != objects.end())
{
object = (*itor).second;
}

But when I check the class values they are all wrong. The only way i
can get this to work is to change the map typedef to use class a but
then class b and class c do not work.

In Java because everything has a common base object, these sort of
problems do not come about but I thought I could do a similar thing by
creating a base class.

How can I do what I want to do or will I have to place pointers to
objects in the map instead of the actual objects?

TIA,
Tom
 
G

Gernot Frisch

If you want a container of base classes that stores derived ones, you
must make a container of base class pointers and store pointers to
derived objects created with "new". Then, you must make sure that
removing from your container calls delete on the item before. The
dectructors _must_ be virtual in this case!
 
T

Tom

Gernot said:
If you want a container of base classes that stores derived ones, you
must make a container of base class pointers and store pointers to
derived objects created with "new". Then, you must make sure that
removing from your container calls delete on the item before. The
dectructors _must_ be virtual in this case!

Thanks, I thought that was going to be the case but I guess I have got
used to using Java now and one of the few things I prefer in Java over
C++ is the use of the base object :)

Then again I have never really liked dealing with pointers but that is
a personal thing based mostly one little things like it's easier to
type a "." than a "->" operator ;)
 
K

Kai-Uwe Bux

Tom said:
Thanks, I thought that was going to be the case but I guess I have got
used to using Java now and one of the few things I prefer in Java over
C++ is the use of the base object :)

Then again I have never really liked dealing with pointers but that is
a personal thing based mostly one little things like it's easier to
type a "." than a "->" operator ;)

Part of the trouble is that we cannot overload the dot operator. Otherwise,
we could write smart references and classes whose objects behave like
polymorphic values (not references). This way, one could take care of many
problems that currently arise with the use of polymorphic types in standard
containers.


Best

Kai-Uwe Bux
 
E

Earl Purple

Gernot said:
If you want a container of base classes that stores derived ones, you
must make a container of base class pointers and store pointers to
derived objects created with "new". Then, you must make sure that
removing from your container calls delete on the item before. The
dectructors _must_ be virtual in this case!

Easiest way is to have a collection of shared_ptr< base > or map< key,
shared_ptr< base > >. shared_ptr is in tr1 and boost. Not yet in std
but will be one day.

Note you cannot use auto_ptr.

boost does also provide some pointer-containers for you. Look up
ptr_containers at www.boost.org
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top