mex
mex

Reputation: 11

stl map with different value types?

I want to store References to different Objects in a map, but I don't know how to define the map.

e.g:

map<string, & ObjectReferenceOfAnyKind> myList;

myList[ "keyA",  stringA );
myList[ "keyBlist",  vector );
myList[ "file",   fileObject );

string &value = (string&) myList["keyA"]; 
CFile &fobj = (CFile&) myList["file"];

Any suggestion how to solve this?

Upvotes: 1

Views: 269

Answers (1)

Peter G.
Peter G.

Reputation: 15144

STL maps (like all STL containers) only store values belonging to a single type. So, you could use pointers to a base class or a union as the value type. Boost offers a modern discriminating union with Variant.

Upvotes: 1

Related Questions