gpt4 book ai didi

c++ - 使用派生参数覆盖函数

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:13 29 4
gpt4 key购买 nike

我想创建一个碰撞系统,其中一个基类代表场景中的一个对象,实现所有的碰撞逻辑,当检测到碰撞时,调用派生类函数进行程序逻辑。我面临的问题是,基类需要了解所有派生类,以便不同函数中的分派(dispatch)能够正常工作。

例子,基类,OnCollisionDetected会被派生类覆盖,处理碰撞

#include <iostream>

class BasePhysicsObject {
public:

void Collides(BasePhysicsObject * another_object) {
/* ... */
bool collides = true;

if (collides) this->OnCollisionDetected(another_object);
return;
}

/* Function to be overriden */
virtual void OnCollisionDetected(BasePhysicsObject * another_object) = 0;
};

场景中的两个虚拟类,覆盖了函数 OnCollisionDetected(BasePhysicsObject * another_object),根据 this 参数将调用分派(dispatch)到适当的函数。

class Fire;

class Player : public BasePhysicsObject {
public:

virtual void OnCollisionDetected(BasePhysicsObject * another_object) {
/* double dispatch to specific implementation */
another_object->OnCollisionDetected(this);
}

virtual void OnCollisionDetected(Fire * fire) {
/* Collision with fire object*/
}
};

class Fire : public BasePhysicsObject {
public:

virtual void OnCollisionDetected(BasePhysicsObject * another_object) {
/* double dispatch to specific implementation */
another_object->OnCollisionDetected(this);
}

virtual void OnCollisionDetected(Player * player) {
/* Collision with player object */
}
};

Main 函数创建两个对象,并检查它们的碰撞。

int main(int argc, char ** argv){

Player * player = new Player();
Fire * fire = new Fire();

fire->Collides(player);
}

最终发生的事情是,从 Collides() 调用的 Fire::OnCollisionDetected(BasePhysicsObject * another_object) 不会调用具有派生类的函数参数即 Player::OnCollisionDetected(Fire * fire),而是 Player::OnCollisionDetected(BasePhysicsObject * another_object) 再次回调该函数,导致堆栈溢出.据我所知,为了使双重分派(dispatch)工作,我需要在所有派生类的基类中声明 OnCollisionDetected(Derived *),但这是一个令人生畏的解决方案。还有其他方法吗?

最佳答案

对于双重调度模式,你必须有一个虚拟调度程序。第一次调度是通过 lhs 实例上的虚拟调用完成的,而不是 rhs 实例的调度:

class BasePhysicsObject {
public:
virtual ~BasePhysicsObject() = default;

virtual void CollideDispatcher(BasePhysicsObject* ) = 0;

// The true collision code.
virtual void OnCollisionDetected(Fire*) = 0;
virtual void OnCollisionDetected(Player*) = 0;
};

class Player : public BasePhysicsObject {
public:
// Always same implementation
// but `this` type is different for each class
// Which allow correct overload resolution
void CollideDispatcher(BasePhysicsObject* rhs) override { rhs->OnCollisionDetected(this); }

void OnCollisionDetected(Fire* rhs) override { /* Player/Fire collision */ }
void OnCollisionDetected(Player* rhs) override { /*Player/Player collision*/ }
};

class Fire : public BasePhysicsObject {
public:
// Always same implementation
// but `this` type is different for each class
// Which allow correct overload resolution
void CollideDispatcher(BasePhysicsObject* rhs) override { rhs->OnCollisionDetected(this); }

void OnCollisionDetected(Fire* rhs) override { /* Fire/Fire collision */ }
virtual void OnCollisionDetected(Player* rhs) override {
// Fire/Player collision:
// might be symmetrical to Player/Fire collision and so:
rhs->OnCollisionDetected(this);
}
};

关于c++ - 使用派生参数覆盖函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51425626/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com