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
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