Understanding double dispatch C++

前端 未结 3 1150
攒了一身酷
攒了一身酷 2020-11-29 04:23

I try to understand how double dispatch works. I created an example where a monster and a warrior derived from the abstract class Creature could fight. The class Creature ha

相关标签:
3条回答
  • 2020-11-29 04:39

    If you want to do this you will need to use RTTI. You will need to check the type of the thing being passed in. In general this is not the best design pattern to be used if you can avoid it. If you want to interact two objects you generally want to use the standard interface of another. For instance you might say creature.attack(other_creature) and attack might query the defense of the other creature, and based on that and it's own stats post an hp update to the other_creature.

    0 讨论(0)
  • 2020-11-29 04:45

    Well, obviously, you really don't have fightwho declared in your Creature class, so you need to declare it there, and declare it as virtual.

    Double dispatch works in a way that for call (this assumes Warrior& w = ..., not Warrior w):

    w.fight(m);
    

    First the virtual mechanism will chose Warrior::fight instead of Monster::fight and then the overloading mechanism will pick Monster::fightwho(Warrior& m) instead of Warrior::fightwho(Warrior& m). Note that it would make more sense if you would have:

    Warrior w;
    Monster m;
    Creature& c1 = w;
    Creature& c2 = m;
    c1.fight(c2); // not w.fight(m)
    

    Therefore, the method which will eventually be called will be dispatched according to type of the object on which you call it and type of the object sent as argument, i.e. double dispatch

    Additionally, please note that this might not be the best example as your types are members of the same hierarchy. Visitor design pattern is a good example of double dispatch implementations in languages which don't support it as first class citizens (i.e. C++ and derivatives: Java, C#...)

    As @CrazyCasta correctly notes, when your class hierarchy starts to grow, this approach becomes much harder to maintain and can result in explosion of number of methods, so choose carefully...

    0 讨论(0)
  • 2020-11-29 04:51

    My contribution to above answers is providing well-tested example in order to clarify double dispatch concept in reality. If you review the below code you will find the answer of how can I implement by myself.

    #include <iostream>
    
    using namespace std;
    
    class A;
    class A1;
    class A2;
    class B1;
    class B2;
    
    class B {
        public:
            // dispatcher function to A
            virtual void collide(const A& a) const = 0;
    
            // actual collision logic B with types of A
            virtual void collide(const A1& a) const = 0;
            virtual void collide(const A2& a) const = 0;
    };
    
    class A {
        public:
            // dispatcher function to B
            virtual void collide(const B& b) const = 0;
    
            // actual collision logic A with types of B
            virtual void collide(const B1& b) const = 0;
            virtual void collide(const B2& b) const = 0;
    };
    
    class A1 : public A {
        public:
            void collide(const B& b) const {
                // dispatch to b
                b.collide(*this);
            }
            void collide(const B1& b) const {
                cout << "collision with B1 and A1" << endl;
            }
            void collide(const B2& b) const {
                cout << "collision with B2 and A1" << endl;
            }
    };
    
    class A2 : public A {
        public:
            void collide(const B& b) const {
                // dispatch to a
                b.collide(*this);
            }
            void collide(const B1& b) const {
                cout << "collision with B1 and A2" << endl;
            }
            void collide(const B2& b) const {
                cout << "collision with B2 and A2" << endl;
            }
    };
    
    class B1 : public B {
        public:
            void collide(const A& b) const {
                b.collide(*this);
            }
            void collide(const A1& b) const {
                cout << "collision with A1 Bnd B1" << endl;
            }
            void collide(const A2& b) const {
                cout << "collision with A2 Bnd B1" << endl;
            }
    };
    
    class B2 : public B {
        public:
            void collide(const A& a) const {
                a.collide(*this);
            }
            void collide(const A1& a) const {
                cout << "collision with A1 Bnd B2" << endl;
            }
            void collide(const A2& a) const {
                cout << "collision with A2 Bnd B2" << endl;
            }
    };
    
    int main() {
    
        A* a = new A1();
        B* b = new B2();
    
        // first dispatch is done by polymorphism ( a is resolved as a A1 )
        // second dispatch is done in collide function by the function overloading
        // ( in collide function we are sending A1 to collide function of B )
        a->collide(*b);
    
    }
    
    0 讨论(0)
提交回复
热议问题