I've been working on a game, and I have a vector called 'people' of a class called 'Person'. The 'Person' class has the virtual method talk, which all of it's derived classes inherit. But when I call the function like so:
people.front().talk();
It call's it's parents class's function instead of the derived class's function. This makes perfect sense to me, because after all it's a vector of 'Person' instead of a vector of 'Bob'. I can't figure out how to avoid this issue, though. Here's some simplified code to show what I mean.
#include <iostream>
#include <vector>
using namespace std;
class Base {
public:
Base() {}
virtual void coutLine() {cout << "base\n";}
};
class Derived : public Base {
public:
using Base::Base;
void coutLine() {cout << "derived\n";}
};
class Owner {
public:
Base *baseItem;
Dude() {}
};
int main() {
vector<Base> stuffs;
Derived d;
stuffs.push_back(d);
Owner chris;
chris.baseItem = &stuffs.front();
chris.baseItem->coutLine();
return 0;
}