C++ 如何有效地使用动态_cast?

C++ 如何有效地使用动态_cast?,c++,dynamic-cast,multiple-dispatch,C++,Dynamic Cast,Multiple Dispatch,有一个抽象类实体,其他类如玩家和敌人都是从中继承的。当游戏检测到实体之间发生碰撞时,将调用以下方法: void handleCollision(Entity* ent1, Entity* ent2) { if (dynamic_cast<Player*>(ent1) || dynamic_cast<Player*>(ent2) && dynamic_cast<Enemy*>(ent1) || dy

有一个抽象类
实体
,其他类如
玩家
敌人
都是从中继承的。当游戏检测到实体之间发生碰撞时,将调用以下方法:

    void handleCollision(Entity* ent1, Entity* ent2) {
        if (dynamic_cast<Player*>(ent1) || dynamic_cast<Player*>(ent2) &&
            dynamic_cast<Enemy*>(ent1) || dynamic_cast<Enemy*>(ent2)) {
            //player <-> enemy collision
        }
        else if (dynamic_cast<Player*>(ent1) || dynamic_cast<Player*>(ent2) &&
                 dynamic_cast<Projectile*>(ent1) || dynamic_cast<Projectile*>(ent2)) {
            //player <-> projectile collision
        }
        else if () {
            //...
        }
        else if() {
            //...
        }
    }
void handleCollision(实体*ent1,实体*ent2){
if(动态|U转换(ent1)|动态|U转换(ent2)&&
动态_-cast(ent1)| |动态_-cast(ent2)){
//玩家与敌人碰撞
}
else if(动态|u cast(ent1)|动态|u cast(ent2)&&
动态_-cast(ent1)| |动态_-cast(ent2)){
//弹丸碰撞
}
如果(){
//...
}
else if(){
//...
}
}

每个实体在与另一个实体碰撞时都有独特的行为,这取决于实体的类型(玩家、敌人等),这就是为什么我需要检查实体之间的每个可能组合,如上图所示。但我不喜欢这样的事实:它创建了一个巨大的else-if链,每个实体都被检查了多次。还有其他方法吗?

使用
实体
类中定义的虚拟函数来唯一标识派生类,无论是玩家还是敌人。这将是避免任何运行时错误的良好实践

 enum EntityType { Entity, Player, Enemy}
在Entity类中,定义这样的虚拟函数

virtual EntityType getType (return Entity;)

并相应地重写两个类中的函数。

尝试扩展Ben Voigt关于多个虚拟分派的评论,大致如下:

void handleCollision(Entity* ent1, Entity* ent2)
{
  ent1->collide_with(ent2);
}
其中:

class Entity
{
 public:
  virtual void collide_with(Entity*) = 0; // Dispatcher

  virtual void handle_collision_with(Entity*) {}
  virtual void handle_collision_with(class Player*) {}
  virtual void handle_collision_with(class Enemy*) {}
  virtual void handle_collision_with(class Projectile*) {}
};


class Player : public Entity
{
public:
  virtual void collide_with(Entity* other) override
   {
    other->handle_collision_with(this);
   }

  virtual void handle_collision_with(Entity* other) override
   {
    // Unhandled entity
   }

  virtual void handle_collision_with(Player* other) override
   {
    // Handle collision player-player
   }

  virtual void handle_collision_with(Projectile* projectile) override
   {
    // Handle collision player-projectile
   }
};

class Enemy : public Entity
{
public:
  virtual void collide_with(Entity* other) override
   {
    other->handle_collision_with(this);
   }

  virtual void handle_collision_with(Enemy* other) override
   {
    // Handle collision enemy-enemy
   }

  virtual void handle_collision_with(Player* player) override
   {
    // Handle collision enemy-player
   }

  virtual void handle_collision_with(Projectile* projectile) override
   {
    // Handle collision enemy-projectile
   }
};

class Projectile : public Entity
{...}

来源:

您不想在这里看到,这些是下载…请阅读“多个虚拟调度”不要使用动态播放。使用虚拟函数。或者
std::unordered_map
我怀疑那些
if'
语句没有达到预期的效果<代码>a | | b和&c | | d的编译方式与编写代码>a | |(b和&c)| | d的方式相同。我猜它应该是
(a | | b)和&(c | | d)