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);
No, the
Derived
objects will be sliced: all additional members will be discarded.Instead of raw pointers, use
std::vector<std::unique_ptr<Base> >
.