01 // PROJECT AT A GLANCE
C++ POOL — DAY 14 MORNING
C++Recovering the real type of an object hidden behind an abstraction — without getting it wrong.
When you handle objects through a common interface, you deliberately lose the information about their real type. Sometimes you have to recover it: this basket contains fruit, but which ones are lemons? C++ offers several tools for that, only one of which is genuinely safe — and the morning consists in understanding why.
dynamic_cast applied to a Fruit / Banana / Lemon / Lime / Coconut hierarchy stored in a FruitBox (a linked list of FruitNode), plus const_cast to regain write access to an array of Coconut declared doubly const.
PRIMARYC++
DELIVERYTEAM RECORD
VERIFICATIONgcc/g++
SOURCEINSPECTOR READY
03 // KEY OUTCOMES
- A reasoned choice of cast rather than the most permissive one
- Frequent downcasting as a symptom of poor design
04 // SOURCE TREE
READING SOURCE…