I have one more question about decoration of c++ classes. With decorators I change virtual int get()
function. But in NPC class I also call the get()
function to get some values for calculation. Is there a way to call Elite::get() inside NPC. I want to get 100 and not 10. Thank you.
class AbstractNPC {
public:
virtual int get() = 0;
};
class NPC: public AbstractNPC {
public:
NPC() { }
int get(){ return 10; }
calc(){ int i = get(); }
};
class NPCDecorator: public AbstractNPC {
private:
AbstractNPC * npc;
public:
NPCDecorator(AbstractNPC *n) { npc = n; }
int get() { npc->get(); }
};
class Elite: public NPCDecorator {
public:
Elite(AbstractNPC *n): NPCDecorator(n) { }
int get() { return 100; }
};
Or is there a better way? Maybe the use of function pointers?