Using dynamic_cast Properly

Programmers who use dynamic_cast sometime discover that their applications suffer from mysterious crashes although seemingly, they are bug free. In most cases, the reason is that they forgot to turn on RTTI support when building the application. For RTTI to operate, the compiler must generate additional code and data in the object file. Most compilers don’t generate this code by default because of its incurred performance overhead. Consequently, any attempt to use dynamic_cast causes a runtime crash because the implementation can’t locate the necessary runtime type information to perform the cast. Therefore, remember always to turn on your compiler’s RTTI toggle when you build an application that uses RTTI features such as dynamic_cast and typeid.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: