I love declaring variables on the stack, especially when using the standard container. Each time you avoir a new
, you avoid a potential memory leak.
I also like using polymorphism, ie class hierarchies with virtual functions. However, it seems these features are a bit incompatible: you can't do:
std::vector<BaseType> vec;
vec.push_back(DerivedType())
or at least it seems you would lose the polymorphic nature of the object you push in.
So, is there any way to reconcile stack-based memory management and the use of virtual functions?