You can make the mapped type be designed to contain one of several types. A C union, for instance, or a boost::variant or boost::any in the C++ case.
Alternatively, you can map to a pointer to a base class (or smart pointer, such as a boost::shared_ptr) and then put classes derived from it into the map.
Which approach is better, or if yet another one should be sought, depends on your high-level requirements.
It depends on the set of data types you want to store. If they are simple, you can make union as advised by Lindley.
If data types are complex, make a base class, and derive other 'data-type' classes from it. Make an object of map like:
Code:
std::map<int, Base*>
In this case, however, you need to allocate appropriate class object before inserting to map. Also, you need to delete the class object after you remove element from map. Make the destructor virtual.
Bookmarks