简体   繁体   English

派生类的函数未被调用

[英]Derived classes' functions not being called

I am dealing with the base class Entity, and I want its derived classes (Player, Enemy, Bullet) to have collideWith() called 我正在处理基类Entity,我希望其派生类(Player,Enemy,Bullet)具有collideWith(),

I have attempted to get the derived functions of Entity's collideWith() to work, however, the base version is always called, which happens to be empty, even when I removed the keyword virtual 我试图使Entity的collideWith()的派生函数正常工作,但是始终会调用基本版本,即使删除了关键字virtual,它也总是为空。

Base class Entity 基类实体

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

and its function, which is always called during collision check 及其功能,通常在碰撞检查期间调用

void Entity::collideWith(Entity*){ 

} 

Derived classes with collideWith function, without virtual keyword These never get called during collision check 具有collideWith函数的派生类,没有虚拟关键字这些在碰撞检查期间永远不会被调用

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

Function for checking collisions p and q points to Entity* from EntityList, which contains its derived classes Player, Enemy, and Bullet 用于检查碰撞p和q的函数从EntityList指向Entity *,其中包含其派生类Player,Enemy和Bullet

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);

                    }


                }
            }
        }
    }

}

The problem is that that you're trying to make C++ do multiple dispatch for you, which it doesn't do, quite simply. 问题在于,您试图使C ++为您执行多次分派 ,而这样做却非常简单。

Methods with the same name but different argument types are, for all intents and purposes, completely different methods, and as such do not override each other. 具有相同名称但参数类型不同的方法,出于所有意图和目的,是完全不同的方法,因此不会相互覆盖。 Since your q variable is likely of type Entity * , the method call will resolve statically as a call to Entity::collideWith(Entity *) , and all the other methods are ignored entirely. 由于您的q变量可能是Entity *类型,因此该方法调用将作为对Entity::collideWith(Entity *)的调用进行静态解析,而所有其他方法将被完全忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM