可以在std容器中使用boost :: smart_ptr吗?

时间:2011-01-21 21:18:39

标签: c++ boost polymorphism containers

可以在std容器中使用boost :: smart_ptr,例如scoped_ptr和shared_ptr,例如std :: map?

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

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

1 个答案:

答案 0 :(得分:20)

scoped_ptr不能在标准容器中使用,因为它无法复制(容器的接口需要)。但是,可以使用shared_ptr

如果您不能使用C ++ 11并且您已经使用了boost,请考虑pointer containers,它的性能会比共享指针容器好一些。

如果你正在使用C ++ 11,请考虑一个unique_ptr的容器,它应该与boost的指针容器类似地执行。

相关问题