正在调用运算符()语法

Calling operator() syntax

本文关键字:语法 运算符 调用      更新时间:2023-10-16

ProjectileManager继承自EntityManager,EntityManager将其作为受保护成员:

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
    }
}

}

如何在it上调用EntityDeallocate?我尝试过EntityDeallocate(it),但它表示it是一个未引用的局部变量。

说明什么是未引用的局部变量?张贴错误字符串,而不是错误字符串的近似值。。

至于如何调用非静态成员函数,它们总是相同的。您需要一个成员函数和一个将其绑定到的对象。

struct Fred
{
    operator()(){}
}
//later on...
Fred fred;
fred();

虽然与您的问题不直接相关,但您可能会发现此链接对理解C++如何调用成员函数非常有帮助。http://www.parashift.com/c++-faq lite/pointers-to-members.html