I'm curious to know what happens when compiling code with a dynamic cast whith RTTI disabled
(either with -fno-rtti
on GCC or with /GR-
on visual studio). Does the compiler "falls back" to static_cast
? Since (at least on VS) it does only issue a warning, what will the compiled code do ?
More specifically, what bad things could happen if I compile without RTTI a code where I'm sure that there are no error possible with dynamic_cast (i.e. where dynamic_cast
could be safely replaced by a static_cast
) like this one :
class A{ /*...*/ } ;
class B : public A {
int foo() { return 42 ;}
} ;
//...
A * myA = new B() ;
int bar = (dynamic_cast<B*>(myA))->foo() ;
See Question&Answers more detail:os