1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. typeid(*basePtr) is evaluated where Base has no virtual…

Added 30 Nov -0001

typeid(*basePtr) is evaluated where Base has no virtual functions and the object is really a Derived. What type is reported?

  1. The static type Base, because typeid ignores the dynamic type here Correct answer
  2. The dynamic type Derived, since typeid always inspects the vtable
  3. A std::bad_typeid exception, since the base class is not polymorphic
  4. The most-derived type, but only after a dynamic_cast has been performed

Correct answer

A. The static type Base, because typeid ignores the dynamic type here

21 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs