What's the preferred C++ idiom to own a collection of polymorphic objects?

后端 未结 4 805
囚心锁ツ
囚心锁ツ 2021-02-02 13:58

Consider the following classes

class Base {
public:
    virtual void do_stuff() = 0;
};

class Derived : public Base {
public
    virtual void do_stuff() { std::         


        
4条回答
  •  长情又很酷
    2021-02-02 14:03

    Assuming from your context that Owner is the sole owner of the contained objects,T should be unique_ptr (where unique_ptr comes from boost or std depending on your C++11 availability). This properly recognizes that it's solely owned by the container and additionally shows the ownership transferral semantics in your add_item call.

提交回复
热议问题