首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++11风格的回调?

C++11风格的回调?
EN

Stack Overflow用户
提问于 2012-09-09 19:31:13
回答 2查看 46K关注 0票数 38

我在一个类中有一个void函数。在旧的C++中,我会把一个函数设为静态的,将类名作为参数,并且有我自己的类,这个类接受一个静态的空函数+一个空*,这样我就可以轻松地调用它。

然而,这感觉很老套。它也没有模板化,这让我觉得我可以做更多的事情。创建myclassVar.voidReturnVoidParamFunc回调的更现代方法是什么

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-09 19:45:28

使用std::function and lambdas (or std::bind())存储可调用代码:

代码语言:javascript
复制
#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();
}      

结果:

之类的!

BIM!

布姆!

胡说八道!

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

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

还要注意,等效的boost::function and boost::bind已经存在多年了。

票数 68
EN

Stack Overflow用户

发布于 2013-05-15 10:31:29

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

代码语言:javascript
复制
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);

    }
}      
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12338695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档