如何std :: bind智能指针返回方法? [英] How to std::bind a smart pointer return method?

查看:375
本文介绍了如何std :: bind智能指针返回方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的Bar类中有此方法:

So I have this method inside my Bar class:

std::shared_ptr<sf::Sprite> Bar::getStuff() const
{
   //...
}

我有我的回调typedef:

And I have my callback typedef:

typedef std::function<void()> Callback;

void Foo::registerCallback(const Callback& callback)
{
    //...
}

现在我想在此方法上使用std::bind,例如:

And now I want to use std::bind on this method, like:

Foo foo;
Bar bar; //I create an instance of an Bar, called bar. 

foo.registerCallback(std::bind(&Bar::getStuff, std::ref(bar))); //<--- ERROR!!!!

错误:

error C2562: 'std::_Callable_obj<std::_Bind<true,std::shared_ptr<sf::Sprite>,std::_Pmf_wrap<std::shared_ptr<sf::Sprite> (__thiscall Bar::* )(void) 

如果我想使用void方法,可以.但是我需要使用getStuff()方法,该方法将把smart pointer返回给sf :: Sprite东西.

If I want to use a void method, it's ok. But I need to use the getStuff() method, which will return me a smart pointer to a sf::Sprite thing.

我该如何实现?

推荐答案

鉴于您使用的是c ++ 11,为什么不使用lambda?

Given you are using c++11, why not a lambda?

foo.registerCallback([&bar]() { bar.getStuff(); });

这篇关于如何std :: bind智能指针返回方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆