开发者

Noob boost::bind member function callback question

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

usi开发者_StackOverflow社区ng 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.


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();

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜