Problem mit RTTI und typeid in Klassenhierachy
-
Es ist folgendermaßen:
Ich möchte ein Kartenspiel mit Hilfe von mvc schreiben.
Momentan entwickle ich das Model (View und Control kann ich noch nicht).
Im Kartenspiel gibts Kreaturen (vorerst), die alle div. Elemente haben.
Hierzu folgende Header-Datei:#ifndef ELEMENTS_H #define ELEMENTS_H #include "CGFramework.h" class Element : public Base<true> { public: Element* Weaker; Element* Weaker2; }; class all : public Element{}; class none : public Element{}; class Ice; class Fire; class Water; class Lightning; class Nature; class Stone; class Earth; class Fire : public Element { Ice* Weaker; Nature* Weaker2; }; class Water : public Element { Fire* Weaker; Stone* Weaker2; }; class Light : public Element { all* Weaker; all* Weaker2; }; class Darkness : public Element { all* Weaker; all* Weaker2; }; class Metal : public Element { all* Weaker; all* Weaker2; }; class Nature : public Element { Earth* Weaker; Water* Weaker2; }; class Earth : public Element { Lightning* Weaker; Stone* Weaker2; }; class Stone : public Element { Fire* Weaker; Lightning* Weaker2; }; class Ice : public Element { Nature* Weaker; Earth* Weaker2; }; class Lightning : public Element { Water* Weaker; Ice* Weaker2; }; class Normal : public Element { none* Weaker; none* Weaker2; }; #endif
Base true sieht so aus:
template <> class Base<true> { protected: virtual inline void del(){} private: inline ~Base(){} public: Base(){} virtual void destroy(){del(); delete this;} friend void* operator new(size_t); friend void operator delete(void*, size_t); };
Dann gibts die Creaturen etc. . Wenn es aber zu einem Kampf zwischen Kreaturen kommt gibts die Klasse Arbitrator, die den Kampf berechnet:
class Arbitrator : public Base<false> { public: void fight(Creature* target, Creature* attacker); }; void Arbitrator::fight(Creature* target, Creature* attacker) { if(target->elment == attacker->elment) { target->LP = target->LP - (attacker->strength - target->armor); } else if(typeid(*target->elment->Weaker) == typeid(*attacker) || typeid(*target->elment->Weaker2) == typeid(*attacker)) { int temp = target->LP - (attacker->strength - target->armor); temp += (temp*10)/100; target->LP = temp; } else if(typeid(*attacker->elment->Weaker) == typeid(*target) || typeid(*attacker->elment->Weaker2) == typeid(*target)) { int temp = target->LP - (attacker->strength - target->armor); temp -= (temp*10)/100; target->LP = temp; } else if(typeid(*target->elment->Weaker) == typeid(all) && typeid(*attacker->elment->Weaker) != typeid(all)) { int temp = target->LP - (attacker->strength - target->armor); temp += (temp*10)/100; target->LP = temp; } else if(typeid(*attacker->elment->Weaker) == typeid(all) && typeid(*target->elment->Weaker) != typeid(all)) { int temp = target->LP - (attacker->strength - target->armor); temp -= (temp*10)/100; target->LP = temp; } else if(typeid(*target->elment->Weaker) == typeid(all) && typeid(*attacker->elment->Weaker)== typeid(all)) { target->LP = target->LP - (attacker->strength - target->armor); } else { target->LP = target->LP - (attacker->strength - target->armor); } }
Wie man sieht hab ich mich von Shade zu typeid bekehren lassen
.
Und hier Creature:
class Creature : public Card { friend class Arbitrator; protected: int strength, armor, LP; Element* elment; public: Creature(){} virtual void attack(Creature* opponent); virtual void del(){delete elment;} }; void Creature::attack(Creature* opponent) { Chief.fight(opponent, this); }
Card sieht so aus:
class Card{} //Alibi-Basisklasse für alle Karten
Dann hab ich das ganze in ne DLL-gepackt und getestet, dabei musste ich noch RTTI bei meinem Compiler aktivieren(ich dacxhte, das wäre als Standard Teil von C++ ???).
Naja ich fang ne exception:
Bad read pointer - no RTTI data!Woher kommt das und wie kann man das ändern?
mfg
-
wat issn RTTI?
-
*** schrieb:
wat issn RTTI?
Run-Time Type Information?