The thing is like this, I have a base class, and I implemented the derived class in two different ways, but the calling method of these two derived class object is same, I would like to push these two derived classes objects into a vector so that I could call them by their index:
class Base {
public:
virtual int operator()()=0;
};
class Derived1: public Base {
public:
int operator()() override {
return 1;
}
};
class Derived2: public Base {
public:
int operator()() override {
return 2;
}
};
int main(void)
{
vector<Base> list;
list.push_back(Derived1());
list.push_back(Derived2());
cout << list[0]();
return 0;
}
But this does not work, since vector will not recognize derived class as the base class. How could I make it work please ?