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);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…