typeid(*basePtr) is evaluated where Base has no virtual functions and the object is really a Derived. What type is reported?
Correct answer
A. The static type Base, because typeid ignores the dynamic type here
21 views
A. The static type Base, because typeid ignores the dynamic type here
21 views
typeid yields the dynamic type only when its operand is a glvalue of polymorphic class type; otherwise it yields the static type of the expression, so here it reports Base. std::bad_typeid is thrown only when the operand is a dereferenced null pointer to a polymorphic type. Making Base polymorphic, typically by adding a virtual destructor, is what enables RTTI-based type identification and dynamic_cast.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.