• Advertisement
Sign in to follow this  

C++ Multiple Inheritance and Method Parameters (**SOLVED**)

This topic is 3549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I'm having trouble with, specifically, collision detection. I want to have my entities capable of handling collisions with specific types of other entities by overloading my "Collides" method with several methods that each take a different class's pointer as their parameter. I'm having a little trouble, though. This is a skeleton overview of my code's entity class structure.
class Entity
{
   // Entity stuff here
};

class MovingEntity : virtual public Entity
{
   // Stuff here for moving entities
};

class TemporaryEntity : virtual public Entity
{
   // Stuff here for entities that only last for a timer
};

class DirectedEntity : virtual public Entity
{
   // Stuff here for entities with a facing and angle direction
};

class Spaceship : virtual public MovingEntity, virtual public DirectedEntity
{
public:
   // Constructor, destructor, etc...

   void Collides( Entity *pOther )
   {
      // Catch-all, doesn't do anything
   }

   void Collides( Projectile *pOther )
   {
      // Should trigger when the other object is a projectile, and be able to access projectile-specific methods
   }
};

class Projectile : virtual public MovingEntity, virtual public TemporaryEntity
{
public:
   // Constructor, destructor, etc.

   void Collides( Entity *pOther )
   {
      // Catch-all, doesn't do anything
   }

   void Collides( Ship *pOther )
   {
      // Should trigger when the other object is a ship, and be able to access ship-specific methods
   }
};



Now, the only time 'Collides' will ever be called, is when it's called by the Entity Manager, which only maintains all of the entities in a std::list<Entity *>, so naturally it calls all of the Collides() methods with a parameter of type Entity*, even though they -may- be Ship* or Projectile*. Is there any way to ensure that these calls always pick the type's farthest-down inheritance before falling back to the catch-all? I'm not really familiar with boost or concepts like reinterpret_cast, but I have a feeling that that my answer lies down that road. Thanks for any help! [Edited by - Spinewire on August 2, 2008 9:33:55 PM]

Share this post


Link to post
Share on other sites
Advertisement
The classic "solution" to this sort of problem centers around an idea called "double dispatch". I put "solution" in quotes because often the real solution doesn't use double dispatch, as it can be a real pain to maintain. Do a gamedev search on "double dispatch" and quite a few good threads come up (most of them relating to collision detection as well).

Share this post


Link to post
Share on other sites
In C++, method calls are polymorphic only for the this pointer, not for the parameters. You can use the visitor pattern or some sort of (type,type)->(function pointer) map.

Share this post


Link to post
Share on other sites
class Entity
{
// Entity stuff here
};

class MovingEntity : virtual public Entity
{
// Stuff here for moving entities
};

class TemporaryEntity : virtual public Entity
{
// Stuff here for entities that only last for a timer
};

class DirectedEntity : virtual public Entity
{
// Stuff here for entities with a facing and angle direction
};

class Spaceship : virtual public MovingEntity, virtual public DirectedEntity
{
public:
// Constructor, destructor, etc...

void Collides( Entity *pOther )
{
// Catch-all, doesn't do anything
}

void Collides( Projectile *pOther )
{
// Should trigger when the other object is a projectile, and be able to access projectile-specific methods
}
};

class Projectile : virtual public MovingEntity, virtual public TemporaryEntity
{
public:
// Constructor, destructor, etc.

void Collides( Entity *pOther )
{
// Catch-all, doesn't do anything
}

void Collides( Ship *pOther )
{
// Should trigger when the other object is a ship, and be able to access ship-specific methods
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement