Reputation: 5885
How to create a std::map
for all kinds of shared_ptrs?
I need a map (key is string
) contains different type shared_ptr
s, for example:
XXXXXX myMap = {
{"B", make_shared<B>()},
{"C", make_shared<C>()}
};
class B
and C
has no relationship.
How to define myMap
?
Upvotes: 0
Views: 316
Reputation: 13698
std::shared_ptr
supports semantic of raw pointers. And all raw pointers can be converted to void*
. So to create a map
containing shared_ptr
of any type you should use the following: std::map<std::string, std::shared_ptr<void>>
Upvotes: 2