Bloomberg L.P. Interview Question: (Programming Language Test) W... | Glassdoor

Interview Question

Financial Applications Engineer Interview New York, NY

(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?
Tags:
technical, oop, c++ programming
Answer

Interview Answer

3 Answers

0

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

Interview Candidate on Jul 1, 2009
2

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.

Gary Y on Sep 18, 2009
3

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.

Anonymous on Jan 8, 2010

Add Answers or Comments

To comment on this, Sign In or Sign Up.