mercredi 7 mars 2018

Ways to replace dynamic_cast in such a situation

Common code, you cant use any specific classes here

class A {
    virtual void foo() = 0;
};

class B {
    virtual void bar(A* a) = 0;
};

And not common code, use what ever classes you want here

class AA : public A
{
    void foo() override {};
    void specificMethodNotAllowedForCommonCode() {};
};

class BB : public B
{
    void bar(A* a) override
    {
        AA *aa = dynamic_cast<AA*>(a);

        aa->specificMethodNotAllowedForCommonCode();
    };
};

I call bar in common code, but the logic every time should be different due the specific realizations of B class's children.

Are there any patterns, which allows me to replace dynamic_cast<> here?

Aucun commentaire:

Enregistrer un commentaire