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
The most obvious solution would be to use std::unique_ptr:
class IBase {};
class Derived : virtual public IBase {};
std::vector> v;
v.push_back(std::unique_ptr(new Derived()));
You could use std::shared_ptr, but it's ownership semantics significantly change the program's behaviour, keeping the dynamically allocated objects alive alive until nobody holds references to them.