c++ downcasting with dynamic_cast

클래스 정의

class Base {
    public:
        virtual const char *getAlgorithm() = 0;
    };

class Derive : public Base {
    public:
        const char *getAlgorithm() {
            return "derive";
        }
    };

class Derive2 : public Base {
    public:
        const char *getAlgorithm() {
            return "derive2";
        }
    };

downcasting

Base *b1 = new Derive();
Base *b2 = new Derive2();

Derive *d1 = dynamic_cast<derive *> (b1); // b1
Derive *d2 = dynamic_cast<derive *> (b2); // null

restriction base class must be virtual class