gpt4 book ai didi

c++ - boost::smart_ptr 可以用在 std 容器中吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:34 25 4
gpt4 key购买 nike

scoped_ptr、shared_ptr等boost::smart_ptr可以用在std::map等std容器中吗?

class SomeClass
{
std::map<int,boost::scoped_ptr<SomeOtherClass> > a_map;
};

作为boost::smart_ptr can be used for polymorphism ,在这种情况下也是如此吗?容器的销毁会触发子类的正确销毁吗?

最佳答案

scoped_ptr 不能在标准容器中使用,因为它不能被复制(这是容器接口(interface)所要求的)。但是,可以使用 shared_ptr

如果您不能使用 C++11 而您已经在使用 boost,请考虑 pointer containers这将比共享指针的容器表现得更好。

如果您使用的是 C++11,请考虑 unique_ptr 的容器,它的性能应该类似于 boost 的指针容器。

关于c++ - boost::smart_ptr 可以用在 std 容器中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4763906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com