I'm currently reading through a C++ tutorial, and for the most part it's going great - but I am slightly confused about the purpose of 'interface classes', and abstract classes.

I don't understand why it is preferable to create a purely virtual base class, only to overwrite all of the methods in the derived ones. I suppose it creates a nice little framework for classes that inherit from it - but I am still quite lost as to the point of it all. Also I'm unsure why it's useful to have a base class pointer bound to a derived class. Could anyone help me to understand it please?

-Thanks in advance

I don't understand why it is preferable to create a purely virtual base class, only to overwrite all of the methods in the derived ones.

I'm doing this in a game-project I'm working on, and when you get used to it it's quite awesome.

My enemies (Soldiers) tick-function makes sure they are running the corrent animation, and so forth - nothing but logic and collision detection.

For making the AI-decisions, I have several different states. Idle, Attack and Flee. The AIStateBase class looks something like this (don't have the code now):

class AIStateBase
{
virtual void onEnter() {} //OVERRIDE ONLY
virtual void update() {}  //OVERRIDE ONLY
virtual void onExit() {}  //OVERRIDE ONLY
};

These are overwritten with firing a gun, running away, ray-casting and various other mechanics in the specific states, namely AIStateIdle, AIStateAttack and AIStateFlee.

Within the Soldier's update function, I'm calling _state->update(); each frame, which will be performed regardless of the subclass - as they all derive from the same superclass.

Upon changing state (player spotted, damage taken, etc), this piece of code is called:

_state->onExit();
delete _state;
_state = new AIStateAttack();
_state->onEnter();

Hope this was clear :-)

The Base Class pointer allows you to do many different things that can surely be used in different ways.

1) As Admiral Pimms suggested, you could change the state of the objects by keeping the whole function same.
2) You could have a std::vector<Base_Class*> and then store Derieved Class Objects within a single Container.

These are 2 things that I can think of.

This article has been dead for over six months. Start a new discussion instead.