c++: can vector<Base> contain objects of type Derived?
c++, object-slicing, polymorphism, vector
Solution
No, the `Derived` objects will be sliced: all additional members will be discarded.
Instead of raw pointers, use `std::vector<std::unique_ptr<Base> >`.
Problem
The title pretty much says it all. Basically, is it legal to do this: ``` class Base { //stuff } class Derived: public Base { //more stuff } vector<Base> foo; Derived bar; foo.push_back(bar); ``` Based on other posts I've seen, the following is okay, but I don't want to use pointers in this case because it's harder to make it thread safe. ``` vector<Base*> foo; Derived* bar = new Derived; foo.push_back(bar); ```