将成员函数作为回调传递,boost :: bind,boost :: function

aud*_*dio 1 c++ boost callback boost-function

在调查使用boost :: bind和boost :: function将成员函数作为回调传递的可能性时,我偶然发现了一个好奇心.我正在为两个班级的模型搞错.第一个(Organism)通过int(void)函数(getAge)公开其成员变量(age).第二类(生物学家)将boost :: function存储为成员(callbackFunction)并使用它来确定(takeNotes)它正在研究的动物的当前年龄(它在成员变量m_notes中保留年龄).第二类的实例(steve_irwin)应该"观察"(takeNotes)第一类的实例(动物).

以下是实现Animal类的代码:

class Organism {
public:
    Organism(int = 1);
    void growOlder();
    int getAge(void);
    void tellAge(void);
private:
    int m_age;
};

Organism::Organism(int _age) : m_age(_age) {}

void Organism::growOlder() {
    m_age++;
}

int Organism::getAge(void) {
    return m_age;
}

void Organism::tellAge(void) {
    std::cout << "This animal is : " << m_age << " years old!";
}
Run Code Online (Sandbox Code Playgroud)

而这是实施生物学家课程的代码:

class Biologist {
public:
    void setCallback(boost::function<int(void)>);
    void takeNotes();
    void tellAge();
private:
    boost::function<int(void)> updateCallback;
    int m_notes;
};

void Biologist::setCallback(boost::function<int(void)> _f) {
    updateCallback = _f;
}

void Biologist::takeNotes() {
    m_notes = updateCallback();
}

void Biologist::tellAge() {
    std::cout << "The animal I am studying is : " << m_notes <<
        " years old! WOW!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

主循环如下:

Organism animal(3);
Biologist steve_irwin;

boost::function<int(void)> f = boost::bind(&Organism::getAge, animal);
steve_irwin.setCallback(f);

steve_irwin.takeNotes();
steve_irwin.tellAge();
animal.tellAge();

animal.growOlder();

steve_irwin.takeNotes();
steve_irwin.tellAge();
animal.tellAge();
Run Code Online (Sandbox Code Playgroud)

我创造了一只3岁的动物,我告诉史蒂夫欧文观看它,他在开始做笔记后正确地说出了它的年龄,但是在动物长大后他再次告诉它的年龄,他仍然认为这只动物是3年旧.

该计划的输出:

The animal I am studying is : 3 years old! WOW!
This animal is : 3 years old!
The animal I am studying is : 3 years old! WOW!
This animal is : 4 years old!
Run Code Online (Sandbox Code Playgroud)

我猜我无法通过引用传递成员函数作为回调,但我无法确定在哪里.你能帮助我吗?

Dog*_*zed 6

而不 boost::function<int(void)> f = boost::bind(&Organism::getAge, animal);应该是boost::function<int(void)> f = boost::bind(&Organism::getAge, &animal);,因为如果你这样做,boost :: bind会创建你对象的内部副本.

请参阅boost :: bind的boost文档