c++ - 如何在基类中存储和执行派生类成员函数

标签 c++ boost pointer-to-member member-functions

所以我正在尝试为我的程序中的一些自动化实体创建一个基本的状态机系统。

这个想法是,自动化实体将简单地调用当前分配给它的任何当前状态或行为。每个状态将分配给 1 个功能。

我的成员函数指针存在不兼容问题。显然不可能简单地调用“派生成员函数指针”,就好像它是“基本成员函数指针”一样。

我相信我需要能够存储某种“通用类成员函数指针”。我一直在阅读许多其他帖子,他们正在谈论使用 boost::bind 和 boost:function 作为一个选项。虽然我不太确定如何在我的代码上下文中使用它:

#include "stdafx.h"
#include <iostream>

using namespace std;

class Automated
{
public:

    typedef void (Automated::*behaviourFunc)();

    void SetBehaviour(behaviourFunc newBehavFunc)
    {
        currentFunction = newBehavFunc;
    }

private:

    behaviourFunc currentFunction;

protected:

    void executeCurrentBehaviour()
    {
        (this->*currentFunction)();
    }
};

class Animal : public Automated
{
public:

    void update()
    {
        executeCurrentBehaviour();
    }
};

class Cat : public Animal
{
    int fishCount;

    void CatchFish()
    { 
        fishCount++;
    }

    void eatFish()
    { 
        fishCount--;
    }
};

class Dog : public Animal
{
    int boneCount;

    void FindBone()
    {
        boneCount++;
    }

    void throwBone()
    {
        boneCount--;
    }

public:

    Dog()
    {
        SetBehaviour(FindBone); //Error: argument of type "void (Dog::*)()" is incompatible with parameter of type "Automated::behaviourFunc"
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Dog jake;
    Cat nemo;

    nemo.SetBehaviour(Cat::CatchFish);  //Error function "Cat::CatchFish" is inaccessible

    jake.update();
    nemo.update();

    return 0;
}

由于我的自动化实体将具有未知数量的状态,因此具有未知数量的功能,因此我无法创建通用虚拟方法。

存储和执行基类的派生类成员函数的最佳方式是什么。

或者,存储通用成员类函数并调用它的方法是什么?

提前致谢。

最佳答案

是的,boost::function 和 boost::bind 正是我要找的东西。

我可以在“自动化”类中存储一个 boost::function。

#include <boost/function.hpp>

class Automated
{
    //ideally there should use a function to set the "currentFunction" but
    //for learning purposes just make it public
    public:

        //function returns void, and no paramters
        boost::function<void()> currentFunction;

        //etc
 }

然后简单地在派生类中调用 boost::bind

#include <boost/bind.hpp>

class Cat : public Animal
{
    int fishCount;

    void CatchFish()
    { 
        fishCount++;
    }

    void eatFish()
    { 
        fishCount--;
    }

    Cat()
    {
        //This bind specifies a void return and no paramters just like the 
        //the signature for the "currentFunction"
        currentFunction = boost::bind(&HF_BattleEnemyBat::CatchFish, this)

       //You can simply call "currentFunction" like this:
       currentFunction();
    }
};

我发现以下链接非常有用。开门见山,在我看来比 boost 文档本身更清楚:

http://www.radmangames.com/programming/how-to-use-boost-function

http://www.radmangames.com/programming/how-to-use-boost-bind

这些链接还详细介绍了如何使用带有参数和不同返回类型的函数。

关于c++ - 如何在基类中存储和执行派生类成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26071402/

相关文章:

c++ - 转换模板中的成员函数指针

c++ - 如何将具有自动返回类型的函数从头文件包含到多个 cpp 文件中

c# - Marshal C++ float to C# float accuracy 问题

c++ - 使用自定义可搜索源 boost::iostreams::stream

c++ - 指向成员的指针集

c++ - 在 C++ 中组合数据成员访问

c++ - SFML:对 _imp_ 的 undefined reference

c++ - .cpp 和 .h 文件中的#ifdefs

c++ - boost::asio UDP 广播

c++ - 将 streambuf 的内容复制到字符串