Following up on this question Vector/Container comprised of different derived objects in C++ I have tried to improve upon my code. Now I am storing pointers to my derived objects in single vector, but I am unsure how to then access their derived-class specific member functions and split the single vector into sub-vectors of each respective derived type.
#include <vector>
#include <memory> // for unique_ptr
#include <iostream>
using namespace std;
class Fruit {};
class Banana: public Fruit { void cout_banana() { cout << "i am a banana" << endl; } };
class Apple : public Fruit { void cout_apple() { cout << "i am an apple" << endl; } };
class FruitBox
{
vector<unique_ptr<Banana>> vec_banana;
vector<unique_ptr<Apple>> vec_apple;
public:
FruitBox(const vector<unique_ptr<Fruit>> &fruits)
{
for (const unique_ptr<Fruit> &f : fruits)
{
// How to figure out if f is Banana or Apple and then
// 1) Print either cout_banana or cout_apple
// 2) Store/Move f in either vec_banana or vec_apple
}
}
};
void main()
{
vector<unique_ptr<Fruit>> inputs;
inputs.emplace_back(new Banana());
inputs.emplace_back(new Apple());
FruitBox fbox = FruitBox(inputs);
}