#include <iostream>
#include <typeinfo>
class Shape { public: virtual ~Shape() {}; };
class Circle : public Shape {};
class Square : public Shape {};
class Other {};
int main() {
Circle c;
Shape* s = &c; // Upcast: normal and OK
// More explicit but unnecessary:
s = static_cast<Shape*>(&c);
// (Since upcasting is such a safe and common
// operation, the cast becomes cluttering)
Circle* cp = nullptr;
Square* sp = nullptr;
// Static Navigation of class hierarchies
// requires extra type information:
// C++ RTTI
if (typeid(*s) == typeid(Circle))
cp = static_cast<Circle*>(s);
if (typeid(*s) == typeid(Square))
sp = static_cast<Square*>(s);
std::cout << "cp = " << cp << std::endl;
std::cout << "sp = " << sp << std::endl;
if (cp) std::cout << "It's a circle!" << std::endl;
if (sp) std::cout << "It's a square!" << std::endl;
// Static navigation is ONLY an efficiency hack;
// dynamic_cast is always safer. However:
// Other* op = static_cast<Other*>(s);
// Conveniently gives an error message, while
// that would be undefined behavior (UB)!
// Other* op2 = (Other*)s;
// does not
// AND *nothing* is guaranteed if we uncomment the UB
// INCLUDING *anything* about the lines *before* the UB (yes, really!)
}