开发者

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&a开发者_运维知识库mp;) myList["file"];

Any suggestion how to solve this?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜