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
There is example with std::vector and std::shared_ptr. I think that's what you want.
#include
#include
#include
class Base {
public:
virtual void foo() {
std::cout << "base" << std::endl;
}
};
class Derived : public Base {
void foo() {
std::cout << "derived" << std::endl;
}
};
int main()
{
std::vector > v;
v.push_back(std::shared_ptr (new Base));
v.push_back(std::shared_ptr (new Derived));
for (auto it : v) {
it->foo();
}
}