c++ - 调用 operator() 语法

标签 c++ operator-overloading sfml

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/

相关文章:

c++ - SFINAE 不能防止不明确的运算符重载吗?

c++ - SFML 2.4.2-绘制文本时出现段错误

c++ - 频率采样滤波器实现

c++ - 如何在 automake 中链接 libavcodec、libavformat?

c++ - C++中赋值运算符重载的目的

python - 重载少于 python

c++ - 静态成员函数使用相同名称时模板类型名称错误

c++ - 定义将对内存进行操作的函数时,将地址传递为 void* 还是 uint8* 更正确?

c++ - 多个 SFML OpenGL 窗口

c++ - SFML 渲染目标.draw : Invalid use of incomplete type