未调用派生的 类' 函数

Derived classes' functions not being called

我正在处理基础 class 实体,我希望它的派生 classes(玩家、敌人、子弹)调用 collideWith()

我试图让 Entity 的 collideWith() 的派生函数工作,但是,总是调用基本版本,它恰好是空的,即使我删除了关键字 virtual

基础class实体

virtual void collideWith(Entity*);
    // Right now the derived classes of collideWIth are not being called,
    // even with the virtual removed

及其函数,在碰撞检查期间总是被调用

void Entity::collideWith(Entity*){ 

} 

派生的 classes 具有 collideWith 函数,没有 virtual 关键字 这些在碰撞检查期间永远不会被调用

void Player::collideWith(Bullet*)
void Player::collideWith(Enemy*)
void Enemy::collideWith(Bullet*)
void Enemy::collideWith(Player*)
void Bullet::collideWith(Player*)
void Bullet::collideWith(Enemy*)

检查碰撞的函数 p 和 q 指向 EntityList 中的 Entity*,其中包含其派生的 classes 玩家、敌人和子弹

void SceneGame::checkCollisions(){

    populateGrid();

    // Right now I am unable to get the collision detection to work!

    for (auto i = 0; i < gridBox.slicesX; ++i){
        for (auto j = 0; j < gridBox.slicesY; ++j){
            if (gridBox.cell[i][j].nEntities < 2) continue;

            for (auto k = 0; k < gridBox.cell[i][j].nEntities; ++k){
                for (auto l = 0; l < gridBox.cell[i][j].nEntities; ++l){

                    // Set up the pointers and compare them
                    auto p = gridBox.cell[i][j].items[k];
                    auto q = gridBox.cell[i][j].items[l];
                    if (p == q) continue; // we do not want the same pointer

                    if (p->getGlobalBounds().
                        intersects(q->getGlobalBounds() )){

                        // Do a series of collisions depending on the specific entities

                        /*
                          However, I end up always calling the BASE function of collideWith
                          instead of the derived types (Player, Enemy, Bullet, etc.)
                        */
                        p->collideWith(q);

                    }


                }
            }
        }
    }

}

问题是您试图让 C++ 为您做 multiple dispatch,而它并没有做,非常简单。

具有相同名称但不同参数类型的方法,就所有意图和目的而言,是完全不同的方法,因此不会相互覆盖。由于您的 q 变量可能属于 Entity * 类型,方法调用将静态解析为对 Entity::collideWith(Entity *) 的调用,并且所有其他方法都将被完全忽略。