I have the following:
typedef std::function<bool (const std::string&)> SomethingCoolCb;
class ClassA
{
public:
void OnSomethingCool(const SomethingCoolCb& cb)
{
_cb = cb;
}
private:
SomethingCoolCb _cb;
};
class ClassB
{
public:
ClassB();
bool Juggle(const std::string& arg);
private:
ClassA _obj;
};
and I want to specify the class function ClassB :: Juggle () as a callback to ClassB :: _ obj. Would there be a proper way to do this in C ++ 11 (in the ClassB constructor):
ClassB::ClassB()
{
_obj.OnDoSomethingCool(
[&](const std::string& arg) -> bool
{
return Juggle(arg);
});
}
From what I understand, the compiler will make the std :: function object from the above lambda code. Therefore, when the callback call is called, it calls the std :: function :: operator () element, and then it will call ClassB :: Juggle () instead of calling the ClassB :: Juggle () class. If I'm not mistaken about what is happening under the covers, everything seems a little ineffective. Is there a better way?
source
share