Yes, polymorphism can be managed without dispatch manually. No, in 2013, no one does this way *, because everyone is tired of reinventing the wheel, and it is better to force the compiler to do something where you may well be mistaken.
The meaning of polymorphism is precisely the fact that you do not need to do a cast and know the runtime type of the object with the potential to make a mistake. With polymorphism, you simply call the method, and work out what you need without your manual control.
If you have only two or three objects, you can do well with a couple of ifs, there are no problems.
But if you have thousands of objects that come from other parts of the program, you will have to either use virtual methods, or make a huge switch before each call, in which you check the type of the object.
Example: you have 10 different classes inherited from a common ancestor. An ancestor has a зафигачить(int сколькоРаз)
method зафигачить(int сколькоРаз)
. You need to trigger the object зафигачить(номерОбъекта)
for all these objects. How to do it? Without polymorphism, your code will look like this:
for (int i = 0; i < objectPointers.size(); i++) { Base* pBase = objectPointers[i]; switch (pbase->type) { case TYPE_CLASS1: { Class1* pClass1 = static_cast<Class1*>(pBase); pClass1->зафигачить(i); } break; case TYPE_CLASS2: { Class2* pClass2 = static_cast<Class2*>(pBase); pClass2->зафигачить(i); } break; // и т. д. } }
With polymorphism, you can easily do this code:
for (int i = 0; i < objectPointers.size(); i++) { objectPointers[i]->зафигачить(i); }
Agree, easier to understand and support. :)
* except in rare cases when they actually write in C, not C ++