I have a class A, which defines an empty function foo, among other things. Two subclasses inherit from A, AA and AB. Both of these override foo in different ways.
A separate class, C, needs to hold different instances of the subclasses, so I have defined a vector with a template of class A within it. The issue is, when I try to run foo by accessing the subclass from the vector in C, it runs the empty version of foo in A, rather than the overridden version from AA or AB.
How can I have a vector in C which can hold different instances of the subclasses and be able to run the overridden version of foo?
If you'd prefer to see the classes in code -
A
class A
{
public:
A();
foo() {}
};
AA
class AA : public A
{
public:
AA();
foo() { //does something else }
};
AB
class AB : public A
{
public:
AB();
foo() { //does a different something else }
};
C
class C
{
public:
C();
private:
std::vector<A> things;
};
From within C, if I try to run things.at(x).foo()
it calls the foo from A, but I need to call the foo from the correct subclass.
Thanks for any advice you can give!