C++ Determine if class can use an object - text RPG game

前端 未结 4 1063
有刺的猬
有刺的猬 2021-01-25 18:58

I\'m facing with the following design problem:

TL;TD need to determine if Hero(class) can use specific object while there\'s many heroes implementations

I have

4条回答
  •  悲哀的现实
    2021-01-25 19:29

    I'm thinking about using Visitor Pattern.

    Something like:

    Hero * h;
    Item * i;
    Visitor v;
    // Assign some values
    h.accept(v,i);
    

    Then the visitor looks like:

    visit(sword *p, warrior * w){
        w.use(p);
    }
    visit(wand *p, wizard * w){
        w.use(p);
    }
    visit(bow *p, archer * w){
        w.use(p);
    }
    

    Any suggestions regard to this idea?

提交回复
热议问题