开发者

stl map with different value types?

开发者 https://www.devze.com 2023-02-20 20:56 出处:网络
I want to store References to different Objects in a map, but I don\'t know how to define the map. e.g:

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

精彩评论

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

关注公众号