Bloomberg Interview Question

(Programming Language Test) When casting an object of a polymorphic class from a base class, which kind of cast executes only if it's is valid?

Interview Answers

Anonymous

Jan 9, 2010

Also, it's important to note the difference between using pointers vs references in the case of casts. In C++, if you use a pointer in a dynamic_cast() and it fails, the result is a null pointer. However, since you cannot have a null reference, dynamic casts need to be wrapped in try/catch blocks.

3

Anonymous

Sep 18, 2009

You should use dynamic cast. It returns a null pointer if it's not valid. Reinterpret will give you a bad pointer since the cast will be pointing to the wrong part of the polymorphic object.

2

Anonymous

Jul 1, 2009

choices were a number of different kinds of casts - dynamic, static, reinterpret, etc.