Let’s say I have an empty class with a virtual function:
class Base
{
public:
virtual void Foo(){std::cout << "this is the base class";}
}
Then I have a class that inherits from Base and overrides Foo():
class Derived : public Base
{
public:
void Foo(){std::cout << "this is the derived class";}
}
Then there is some other class that contains a list of Base:
class OtherClass
{
public:
std::vector<Base> listOfBases; //note it's not std::list<Derived>
}
How do I cycle through the listOfBases and call Foo() for the Derived class not the Base class? Right now if I was to say listOfBases[i].Foo(); then this is the base class would print, but I want the overridden one from the Derived class to print.
I could just make it a list of Derived instead of Base and that would fix it, but I’m going to be calling these inherited class all kinds of different things so I need a list of Base.
So how do I call an overridden function from a list of its base class?
You need to use a list of
Base*(that is, pointers-to-Base), or preferably,std::unique_ptr<Base>orstd::shared_ptr<Base>.The reason for this is because of the C++ object model and copying. Derived classes must be at least as large as their base class (they can be the same size depending on the derived class being empty or not). Since C++ utilizes copying (or possibly moving in C++11) when adding items to a
vector, it must allocate enough space for nBaseobjects. Since avectoris almost always a wrapper around a simplearray, trying to add a (possibly differently sized)Derivedobject into anarrayofBaseobjects is undefined behaviour.