C++ 11风格的回调?

36 c++ c++11

我在类中有一个void函数.在旧的C++中,我将一个函数静态化,将类名作为参数,并拥有自己的类,它使用静态void函数+一个void*来轻松调用它.

然而,感觉老派.它也没有模仿,感觉我可以做得更多.什么是更现代的方法来创建myclassVar.voidReturnVoidParamFunc的回调

Kla*_*aim 63

使用std::function和lambda(或std::bind())来存储callables:

#include <functional>
#include <iostream>


class Test
{
public:
      void blah() { std::cout << "BLAH!" << std::endl; }
};

class Bim
{
public:
      void operator()(){ std::cout << "BIM!" << std::endl; }
};

void boum() { std::cout << "BOUM!" << std::endl; }


int main()
{
    // store the member function of an object:
    Test test;  
    std::function< void() > callback = std::bind( &Test::blah, test );
    callback();

    // store a callable object (by copy)
    callback = Bim{};
    callback();

    // store the address of a static function
    callback = &boum;
    callback();

    // store a copy of a lambda (that is a callable object)
    callback = [&]{ test.blah(); }; // often clearer -and not more expensive- than std::bind()
    callback();
}      
Run Code Online (Sandbox Code Playgroud)

结果:

BLAH!

BIM!

BOUM!

BLAH!

编译并运行:http://ideone.com/T6wVp

std::function可以用作任何可复制对象,因此可以将其作为回调存储在某个地方,就像在对象的成员中一样.这也意味着你可以自由地将它放在标准容器中,比如std::vector< std::function< void () > >.

另请注意,等效的boost :: function和boost :: bind已经有多年了.


Les*_*ieM 7

有关使用Lambda和向量将参数传递给C++ 11回调的示例,请参阅http://ideone.com/tcBCeO或以下内容:

class Test
{
public:
      Test (int testType) : m_testType(testType) {};
      void blah() { std::cout << "BLAH! " << m_testType << std::endl; }
      void blahWithParmeter(std::string p) { std::cout << "BLAH1! Parameter=" << p << std::endl; }
      void blahWithParmeter2(std::string p) { std::cout << "BLAH2! Parameter=" << p << std::endl; }

      private:
         int m_testType;

};

class Bim
{
public:
      void operator()(){ std::cout << "BIM!" << std::endl; }
};

void boum() { std::cout << "BOUM!" << std::endl; }


int main()
{
    // store the member function of an object:
    Test test(7);  
    //std::function< void() > callback = std::bind( &Test::blah, test );
    std::function< void() > callback = std::bind( &Test::blah, test );
    callback();

    // store a callable object (by copy)
    callback = Bim{};
    callback();

    // store the address of a static function
    callback = &boum;
    callback();

    // store a copy of a lambda (that is a callable object)
    callback = [&]{ test.blah(); }; // might be clearer than calling std::bind()
    callback();

    // example of callback with parameter using a vector
    typedef std::function<void(std::string&)> TstringCallback;

    std::vector <TstringCallback> callbackListStringParms;
    callbackListStringParms.push_back( [&] (const std::string& tag) {     test.blahWithParmeter(tag); }); 
    callbackListStringParms.push_back( [&] (const std::string& tag) { test.blahWithParmeter2(tag); }); 

    std::string parm1 = "parm1";
    std::string parm2 = "parm2";
    int i = 0;
    for (auto cb : callbackListStringParms ) 
    {
        ++i;
        if (i == 1)
            cb(parm1);
        else
            cb(parm2);

    }
}      
Run Code Online (Sandbox Code Playgroud)