菜鸟boost::bind成员函数回调问题

发布于 2024-09-03 06:49:48 字数 646 浏览 6 评论 0原文

#include <boost/bind.hpp>
#include <iostream>

using namespace std;
using boost::bind;

class A {
public:
    void print(string &s) {
        cout << s.c_str() << endl;
    }
};


typedef void (*callback)();

class B {
public:
    void set_callback(callback cb) {
        m_cb = cb;
    }

    void do_callback() {
        m_cb();
    }

private:
    callback m_cb;
};

void main() {
    A a;
    B b;
    string s("message");
    b.set_callback(bind(A::print, &a, s));
    b.do_callback();

}

所以我想做的是让 A 流“消息”的 print 方法在 b 的回调被激活时进行计算。我从 msvc10 收到意外数量的参数错误。我确信这是超级新手的基本知识,我提前表示抱歉。

#include <boost/bind.hpp>
#include <iostream>

using namespace std;
using boost::bind;

class A {
public:
    void print(string &s) {
        cout << s.c_str() << endl;
    }
};


typedef void (*callback)();

class B {
public:
    void set_callback(callback cb) {
        m_cb = cb;
    }

    void do_callback() {
        m_cb();
    }

private:
    callback m_cb;
};

void main() {
    A a;
    B b;
    string s("message");
    b.set_callback(bind(A::print, &a, s));
    b.do_callback();

}

So what I'm trying to do is to have the print method of A stream "message" to cout when b's callback is activated. I'm getting an unexpected number of arguments error from msvc10. I'm sure this is super noob basic and I'm sorry in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-09-10 06:49:48

typedef void (*callback)(); 替换为 typedef boost::function;回调;

绑定函数不会生成普通函数,因此您不能将其存储在常规函数指针中。但是,boost::function 能够处理任何事情,只要它可以使用正确的签名进行调用,这就是您想要的。它将与函数指针或使用绑定创建的函子一起使用。

对您的代码进行一些更正后,我想出了这个:

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>

// i prefer explicit namespaces, but that's a matter of preference

class A {
public:
    // prefer const refs to regular refs unless you need to modify the argument!
    void print(const std::string &s) {
        // no need for .c_str() here, cout knows how to output a std::string just fine :-)
        std::cout << s << std::endl;
    }
};


// holds any arity 0 callable "thing" which returns void
typedef boost::function<void()> callback;

class B {
public:
    void set_callback(callback cb) {
        m_cb = cb;
    }

    void do_callback() {
        m_cb();
    }

private:
    callback m_cb;
};

void regular_function() {
    std::cout << "regular!" << std::endl;
}

// the return type for main is int, never anything else
// however, in c++, you may omit the "return 0;" from main (and only main)
// which will have the same effect as if you had a "return 0;" as the last line
// of main
int main() {
    A a;
    B b;
    std::string s("message");

    // you forget the "&" here before A::print!
    b.set_callback(boost::bind(&A::print, &a, s));
    b.do_callback();

    // this will work for regular function pointers too, yay!
    b.set_callback(regular_function);
    b.do_callback();

}

replace typedef void (*callback)(); with typedef boost::function<void()> callback;

A bound function doesn't produce an ordinary function, so you cannot just store it in a regular function pointer. However, boost::function is able to handle anything as long as it is callable with the correct signature, so that's what you want. It will work with a function pointer, or a functor created with bind.

After a few corrections to your code, I came up with this:

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>

// i prefer explicit namespaces, but that's a matter of preference

class A {
public:
    // prefer const refs to regular refs unless you need to modify the argument!
    void print(const std::string &s) {
        // no need for .c_str() here, cout knows how to output a std::string just fine :-)
        std::cout << s << std::endl;
    }
};


// holds any arity 0 callable "thing" which returns void
typedef boost::function<void()> callback;

class B {
public:
    void set_callback(callback cb) {
        m_cb = cb;
    }

    void do_callback() {
        m_cb();
    }

private:
    callback m_cb;
};

void regular_function() {
    std::cout << "regular!" << std::endl;
}

// the return type for main is int, never anything else
// however, in c++, you may omit the "return 0;" from main (and only main)
// which will have the same effect as if you had a "return 0;" as the last line
// of main
int main() {
    A a;
    B b;
    std::string s("message");

    // you forget the "&" here before A::print!
    b.set_callback(boost::bind(&A::print, &a, s));
    b.do_callback();

    // this will work for regular function pointers too, yay!
    b.set_callback(regular_function);
    b.do_callback();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文