Hi everyone!
I'm doing a C++ problem that I'm a bit stuck on.
I'd like to be able to store multiple data types in a C++ container with a key (such as a Map)...
Is there any way to do this or another way I should be approaching this?
Thanks
Hi everyone!
I'm doing a C++ problem that I'm a bit stuck on.
I'd like to be able to store multiple data types in a C++ container with a key (such as a Map)...
Is there any way to do this or another way I should be approaching this?
Thanks
since you can only have one "type", all entries in the map must be of that type. for example, a map with type "string" can only store objects of type "string" (of course).
the only reason you would want to have the map store "different" types is if they are all related somehow, that is: they all extend some class. maybe you have a map to store objects of type "Vehicle". if you have child classes "Sedan", "Truck", etc, then those can be stored in the same map--because they are related. you wouldnt want to add an "Animal" to your "Vehicle" map because they arent related (of course unless you made them!).
if you are trying to store different unrelated types (i.e. no parent-child relationship), then its probably a design issue (or this data structure doesnt make sense to use).
hope it helps.
I agree that putting mixed unrelated types in a container seems a bit strange, but if you really want to, I think using the Boost Any class should work.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010