ProjectileManager 继承自 EntityManager,后者将 this 作为 protected 成员:
struct EntityDeallocator{
void operator()(std::pair<sf::String,std::shared_ptr<Entity>> p)const{
p.second.reset();
std::cout << "object removed" << std::endl;
}
};
ProjectileManager 更新函数:
void ProjectileManager::update(double frameTime){
for(std::map<sf::String,std::shared_ptr<Entity>>::const_iterator it = entities.begin();it!=entities.end();it++){
it->second->update(frameTime);
it->second->getObject()->draw(*SfmlFramework::Singleton()->window);
if(it->second->getObject()->getSprite()->GetPosition().x > SfmlFramework::Singleton()->window->GetWidth() || it->second->getObject()->getSprite()->GetPosition().y > SfmlFramework::Singleton()->window->GetHeight()){
//I want to call EntityDeallocator on it
}
}
我如何在它
上调用 EntityDeallocator?我试过 EntityDeallocator(it) 但上面写着 it
是一个未引用的局部变量。
最佳答案
说什么是未引用的局部变量?发布您的错误字符串,而不是您对错误字符串的近似值。
至于如何调用非静态成员函数,他们总是一样的。您需要一个成员函数和一个将它绑定(bind)到的对象。
struct Fred
{
operator()(){}
}
//later on...
Fred fred;
fred();
虽然不是直接与您的问题相关,但您可能会发现此链接对于理解 C++ 如何调用成员函数非常有帮助。 http://www.parashift.com/c++-faq-lite/pointers-to-members.html
关于c++ - 调用 operator() 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9851889/