Which C++ operator is commonly used for runtime type checking and safe downcasting in a polymorphic class hierarchy?

Options

  • A. static_cast
  • B. dynamic_cast
  • C. reinterpret_cast
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. dynamic_cast

Detailed Explanation

Correct Answer: Option B

Explanation:
dynamic_cast can perform runtime-checked casts in appropriate polymorphic class hierarchies. For a failed pointer downcast it returns nullptr; for a failed reference cast it throws std::bad_cast. static_cast does not provide the same runtime check for downcasting.