Read again: the compiler looks up the ACTUAL type of the object pointed to by the pointer or reference from where you call the function and calls the appropriate function in the appropriate class.
But you are creating a SINGLE character of type... character.
Base code:
Code:
class Creature { virtual void foo(); }
class Monster: public Creature { virtual void foo(); }
class Character: public Creature { virtual void foo(); }
This is not polymorphism:
Code:
void foo()
{
Creature* useless = new Creature;
useless->foo();
delete useless;
}
int main()
{
Monster m1, m2, m3;
Player p1, p2, p3;
foo(); // Calls useless::foo
foo(); // Calls useless::foo
foo(); // Calls useless::foo
foo(); // Calls useless::foo
foo(); // Calls useless::foo
foo(); // Calls useless::foo
}
This is polymorphism:
Code:
void foo(Creature& p)
{
p.foo();
}
int main()
{
Monster m1, m2, m3;
Player p1, p2, p3;
foo(p1); // Calls p1::foo
foo(p2); // Calls p2::foo
foo(p3); // Calls p3::foo
foo(m1); // Calls m1::foo
foo(m2); // Calls m2::foo
foo(m3); // Calls m3::foo
}
Make sure you understand the difference!!
The first example is what YOU are doing right now.