Zach
Zach

Reputation: 5885

Container of shared_ptr with different type?

How to create a std::map for all kinds of shared_ptrs?

I need a map (key is string) contains different type shared_ptrs, 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

Answers (1)

ixSci
ixSci

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

Related Questions