[英]C++: Storing derived class's instance pointers in STL container of containers
I read Passing shared_ptr<Derived> as shared_ptr<Base> but that doesn't answer my question.我读过Passing shared_ptr<Derived> as shared_ptr<Base>但这并不能回答我的问题。
Suppose we have假设我们有
class Base
{
};
class Derived : public Base
{
};
I would like to create the following container: std::map<int, std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Base>>>> _channels;
我想创建以下容器:
std::map<int, std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Base>>>> _channels;
I need to add instances as values with types std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Base>>>
and std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Derived>>>
.我需要将实例添加为类型为
std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Base>>>
和std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Derived>>>
。
Unfortunately I'm unable to add instances with Derived
type, but with type Base
it's fine.不幸的是,我无法添加
Derived
类型的实例,但使用Base
类型就可以了。
Full code:完整代码:
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <memory>
#include <utility>
class Base
{
};
class Derived : public Base
{
};
int main()
{
// works fine
/*
std::map<int, std::shared_ptr<Base>> _channels2;
_channels2.emplace(std::pair<int, std::shared_ptr<Derived>>(3, std::make_shared<Derived>()));
std::cout << "_channels2.size() = " << _channels2.size() << std::endl;
*/
// fails to compile
std::map<int, std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Base>>>> _channels;
_channels.emplace(std::pair<int, std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Derived>>>>(2, std::make_shared<std::unordered_map<std::string, std::shared_ptr<Derived>>>()));
std::cout << "_channels.size() = " << _channels.size() << std::endl;
return 0;
}
I'm using C++14.我正在使用 C++14。
Am I understand well so the reason behind it fails to compile is because std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Derived>>>
is not a subtype of std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Base>>>
?我理解得很好,所以它无法编译的原因是因为
std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Derived>>>
不是std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Base>>>
的子类型std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Base>>>
? So polymorphism isn't available in this case.所以在这种情况下多态性不可用。
Is there a workaround you can advise?有没有可以建议的解决方法?
Thank you.谢谢你。
You could try something considering this works:考虑到这行得通,您可以尝试一些方法:
std::map<std::string, std::shared_ptr<Derived>> derived_map;
std::map<std::string, std::shared_ptr<Base>> base_map(derived_map.begin(), derived_map.end());
thus you can write:因此你可以写:
std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<Derived>>> derived_map;
auto base_map = std::make_shared<std::unordered_map<std::string, std::shared_ptr<Base>>>(derived_map->begin(), derived_map->end());
an so on... the drawback is that only base and derived are "shared", the maps are duplicated.等等......缺点是只有基础和派生是“共享的”,地图是重复的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.