我想制作一个将std::function
作为参数的函数,并在内部调用该函数:
void handleCollision(std::function<void(const Entity&)>& resolveCollision, const Entity& block) {
if (playerInRangeOf(block) && playerIntersects(block)) {
resolveCollision(block);
}
}
和呼叫者(在同一个类中:)>
for (auto&& block : blocks) { handleCollision(resolveCollisionAxisX, block); }
错误:
Reference to non-static member function must be called error: no matching function for call to 'StateGame::handleCollision(<unresolved overloaded function type>, Block&)' handleCollision(resolveCollisionAxisX, block); ^
但是],如果我遵循C风格的函数指针:
void handleCollision(void (StateGame::*resolveCollision)(const Entity&), const Entity& block) { if (playerInRangeOf(block) && playerIntersects(block)) { (this->*resolveCollision)(block); } }
然后它确实可以正常工作(呼叫者是相同的。)>
如何使
std::function
工作?旁注:我不想使任何内容静态化。
我想制作一个将std :: function作为参数的函数,并在内部传递通过函数的调用:void handleCollision(std :: function
handleCollision(resolveCollisionAxisX, block)