Chapter 13 - Worked Exercises 1

Exercise 1

Part 0 - Static Cast

  1. What checks are imposed by the static_cast operator?

  2. Is it at compile time or at runtime that these checks are performed?

  3. Will static_cast always return a value?

  4. There are two scenarios where static_cast should be used, one related to inheritance, the other related to implicit type conversions. Describe these scenarios.

  5. What is upcasting and downcasting?

  6. When should you not downcast an object pointer?

Part 1 - Dynamic Cast

  1. How does dynamic_cast differ to that of static_cast?

  2. What is the result of invoking dynamic_cast?

  3. What is runtime type identification? Show a basic example illustrating this concept.

  4. Why should you always check for the return value of a dynamic_cast operation?

Part 2 - Reinterpret Cast

  1. What is the effect on the underlying value of a variable when performing a reinterpret_cast operation? How does it differ to that of implicit casting?

Part 3 - Const Cast

  1. What is the purpose of const casting? Show a simple example of its usage.

  2. Why should const cast only be used as a last resort? What alternatives should be explored before attempting to use a const cast?

Part 4 - Workshop

Please complete the Workshop section for Chapter 14 in the textbook (pg. 389).

Last updated

Was this helpful?