I need to make a vector containing objects of a base class and a derived class and know which element is which
I need to make a vector containing objects of a base class and a derived class and know which element is which
I am working on a game that has monsters and dragons. Dragons can do everything monsters can do except they can also breathe fire.
I have made a class of type 'monster' and a class that inherits from monster called 'dragon'.
I then have a class called 'monsters' which has as a private member vector which will contain dragon and monster elements.
In the main game loop I need to cycle through the vector and blow fire if the current element is a dragon, and do nothing if it is just a monster.
I have tried using typeid() but it always returns monster* whether the current element is a plain monster or a dragon.
Is there any way to do this or does it not make sense to even use inheritance in this case? Would it make more sense for the dragon class to not inherit, and to instead stand by itself independent of monster?
Any advice is appreciated.
Answer by Seth Carnegie for I need to make a vector containing objects of a base class and a derived class and know which element is which
Not that this is the best way to do it, but just for your information, you were calling typeid
on the pointer which doesn't work if you want the runtime type of the object. typeid
does work at runtime when it needs to, but you need to dereference pointers to polymorphic types:
typeid(*monsterpointer)
And that will get the runtime type of *monsterpointer
, be it a monster
or dragon
.
However, typeid
is only for exact comparisons, so if you add another class called ImpairedDragon
that inherits from Dragon
, the typeid
code will break and not work with them. So you should probably use dynamic_cast
which works with inheritance, or even better, a virtual function if you can think of how to use one well.
Answer by Aesthete for I need to make a vector containing objects of a base class and a derived class and know which element is which
You could do something like this.
class Monster {}; class Dragon : public Monster { public: void BlowFire() {} } std::vector monsters; // Collection of monsters for(auto it = monsters.begin(); it != monsters.end(); ++it) { Dragon* pDragon = dynamic_cast(*it); // Attempt to convert pointer if(pDragon) { pDragon->BlowFire(); // If it is indeed a Dragon, blow fire. } }
But it sounds like you should arrange your classes a little better. Why don't you just have all your monster classes have an Attack
method? This way you can override the methods for each different monster type.
class Monster { public: virtual void Attack() {} // All monsters can attack }; class Dragon : public Monster { public: virtual void Attack() { // Do something special for a Dragon. } }; Monster myMonster; Dragon myDragon; myMonster.Attack(); // Generic monster attack. myDragon.Attack(); // Special super dragon fire attack!
Answer by piokuc for I need to make a vector containing objects of a base class and a derived class and know which element is which
How about putting an empty (no operation) virtual blowFire
in the base class, and then overriding it with something in the derived class?
It would be even better to rename the method to specialAttack
or a similar generic name, so you don't need to add similar methods for other kinds of Monsters.
Answer by Pete Becker for I need to make a vector containing objects of a base class and a derived class and know which element is which
There are two possible approaches: use a virtual function or use a dynamic_cast
. If monster
has a virtual function blow_fire()
that does nothing, dragon
can override it to do whatever is appropriate for a dragon. Then you just call it for all the monster
objects, and the dragons will blow fire. Using dynamic_cast
, the code would be something like this:
if (dragon *dragon_ptr = dynamic_cast(monster_ptr)) { dragon_ptr->blow_fire(); }
That's generally viewed as a bad approach, because it doesn't grow naturally. If you added another type of monster that could blow fire you'd have to add another dynamic_cast
. With a virtual function, the new type would just override the virtual function.
Answer by user1648984 for I need to make a vector containing objects of a base class and a derived class and know which element is which
If you've got monster pointers in your container, then public monster members are all you are able to access through those pointers in a type-safe way (from my recollection). You might try resorting to an interface-based design la COM so that you can query the monster base class for any extra features i.e. interfaces. I'd provide you with a sample but my lunchtime is now over.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment