I am learning polymorphism and I am familiar with php.
I came across this excellent example from https://stackoverflow.com/a/749738/80353. reproduced below.
How
Since you only asked about storing them, I'll omit the implementation of Animal
, Dog
and Cat
.
vector< shared_ptr > animals;
animals.push_back( new Dog("Skip") );
animals.push_back( new Cat("Snowball") );
for( size_t i = 0; i< animals.size(); ++i )
cout << animals[i]->name << " says: " << animals[i]->speak() << endl;
Essentially you need to store pointers to the objects. Put simply, shared_ptr
is a class that stores a pointer and deletes it when it is no longer referenced.