Polymorphism is what allows Irrlicht to work, basically
You use it through virtual methods.
These are necesary because a virtual method will be linked in runtime, instead of statically linked during the compilation time.
If the class had no subclasses, this dynamic linkage would still be done, as useless as it maybe because there is only one definition of the method, if there was no definition, it would give a compilation error. Don't mistake this with DLL's. Stand alone programs still may make use of this kind of linkage.
Then, if the class has derived subclasses, the linked method will be only that of the class that calls it. A pointer to a parent class will give you the method of the parent class, and a pointer to the child class will give you the child's implementation. So far, so good.
And now comes the important part. If ANY of the methods of the parent class is defined as abstract, that is, a virtual method equaled to 0, like this:
the class is abstract, and hence, is considered that the called methods of this class will be always those of the child classes if they are implemented. And any derived class MUST implement this foo method. or else, the dynamic linkage would fail, but this would result just in a compilation failure. This is the polymorphism.
It means that through a pointer to a base class, you get the implementation of the child class, which means that any class derived from the base class may behave just like the base class, and this way, you may unify the behavior of your objects. All act using the same naming convention, but with diferent implementations.
Take a look at this, this is a working example, i use it (more elaborately... but i use it):
Code: Select all
class executionUnit
{
//constructor... nothing in it
executionUnit() {}
virtual ~executionUnit() {}
//it is important to define this virtual even if it is empty, because the destructors have special rules with regard to polymorphism
//they are called in cascade from child to parent class, instead of only that of the child class.
virtual bool init() = 0;
virtual bool run() = 0;
virtual void clean() = 0;
};
this class is abstract, and completely empty...
Now, in my program, i have several classes defined under that base class.
main, screen1, screen2...
well, each of those classes, main, screen1, screen2, implement the init, run and clean methods. Then, in my main i can do this:
Code: Select all
void main()
{
int i = 0;
executionUnit* unit;
//...i create the main,screen1 and screen2 objects...
main->init(); //safe initialization ;)
unit = main; //It is a legal assignment, unit is of the base class of main.
//from now on, we care little of what our unit pointer is aiming to... all of the child classes work the same, isn't it?
while(unit->run())
{
unit->clean();
switch(i%3)
{
case 0:
unit = main;
break;
case 1:
unit = screen1;
break;
case 2:
unit = screen2;
break;
}
unit->init();
++i;
}
//This ends when the unit class returns false in the run call...
//See how we have created a program that runs 3 small "execution units" (hence the name) in "parallel" :)
}
This is polymorphism. I hope you understand it.