#include <stdio.h>
#ifdef WITH_STD
#include <typeinfo>
#endif

class GraphObject {
public:
    virtual ~GraphObject() {}
};

class Circle : public GraphObject {
    int radius;
public:
    Circle(int r) : radius(r) {}
    void Grow() { radius *= 2; }
    int Radius() const { return radius; }
};

class Square : public GraphObject {
};

#ifdef WITH_STD
/* the reference form: failure is an exception from the library */
void grow(GraphObject& obj)
{
    try {
        Circle& c = dynamic_cast<Circle&>(obj);
        c.Grow();
        printf("circle, radius %d\n", c.Radius());
    }
    catch (const std::bad_cast&) {
        printf("not a circle\n");
    }
}
#else
/* the book's advice: address, pointer form, check, dereference */
void grow(GraphObject& obj)
{
    Circle *p = dynamic_cast<Circle*>(&obj);
    if (!p) {
        printf("not a circle\n");
        return;
    }
    Circle& c = *p;
    c.Grow();
    printf("circle, radius %d\n", c.Radius());
}
#endif

int main()
{
    Circle circle(5);
    Square square;
    grow(circle);
    grow(square);
#ifdef UNCAUGHT
    Circle& c = dynamic_cast<Circle&>(static_cast<GraphObject&>(square));
    c.Grow();
#endif
    return 0;
}
