Container that stores a mixture of base and derived objects?

前端 未结 4 1787
说谎
说谎 2021-01-07 03:51

What is the right thing to do? I know that if the container is of base class value type, then derived object stored is \'sliced\'. If container is of derived class type, the

4条回答
  •  天命终不由人
    2021-01-07 04:34

    If you want polymorphic behavior (and you do want it), then you must use pointers or references. That is well documented in many places.

    And since you cannot use containers of references, you have to use containers of pointers.

    Now, you can use any type of pointer you see fit: unique_ptr, shared_ptr or raw pointers.

提交回复
热议问题