RTTI & Polymorphism
I read somewhere that RTTI is acting against Polymorphism?
Could anyone please let me know the reason.
Thanks
Deb
[131 byte] By [
debkumar] at [2007-11-11 7:26:32]

# 1 Re: RTTI & Polymorphism
when you use polymorphic, you must enable RTTI. RTTI is C++ feature to determine object type in run-time.
# 2 Re: RTTI & Polymorphism
As per my understanding, in Polymorphic classes you need to know the type of the object before hand where as in RTTI you do not need to know the type at compile time.
Is my understanding correct?
Regards
Debkumar
# 3 Re: RTTI & Polymorphism
The criticism refers to a programming style that uses switch-blocks to determine to dynamic type of an object and call the relevant member functions:
if (typeid(myobj)==typeid(CWnd))
...do CWnd stuff
else if (typeid(myobj)==typeid(CDilaog))
...do CDialog stuff
This style is the anathema of OOP. It hardwires the object's type to the program's logic, whereas in real OOP you wouldn't care about the dynamic type of myobj. Instead, you would use a virtual member function and override it in the derived classes. This way you could use the same line of code:
myobj.func(); //or pmyobj->func()
Depending only on the static type of myobj. More importantly, this code will work correctly even if you add new derived class to the hierarchy, whereas in the if-else sequence, you always need to add a new case for every new derived class. The use of dynamic_type instead of typeid can solve some of these design problems but it introduces another problem: performance.
Danny at 2007-11-11 21:04:22 >
