#include <iostream>
#include <typeinfo>
struct Shape { virtual ~Shape() {}; };
struct Circle : Shape {};
struct Square : Shape {};
struct Other {};
int main() {
Circle c;
Shape & s = c; // Upcast: normal and OK
// More explicit but unnecessary:
s = static_cast<Shape&>(c);
std::cout << "Address of `s` = " << &s << std::endl;
// (Since upcasting is such a safe and common
// operation, the cast becomes cluttering)
// Static Navigation of class hierarchies
// requires extra type information:
// C++ RTTI
if (typeid(s) == typeid(Circle))
{
Circle & cp = static_cast<Circle&>(s);
std::cout << "It's a circle!" << std::endl;
std::cout << "Address = " << &cp << std::endl;
}
if (typeid(s) == typeid(Square))
{
Square & sp = static_cast<Square&>(s);
std::cout << "It's a square!" << std::endl;
std::cout << "Address = " << &sp << 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!)
}