0

私は友人と一緒に取り組んでいるゲームの簡単な開発者コンソールを作っています。関数をコンソールにバインドする作業を行っているので、コンソールで呼び出す名前を保持する文字列を含む std::map と、 sf::String (私たちは SFML を使用しており、sf は SFML 名前空間です) であり、パラメーターとして sf::String を取ります。すべてのコンソール関数は sf::String を受け取り、sf::String を返します。

問題のコードは次のようになります (すべてのコードではありません)。

#include <SFML/System/String.hpp>
using namespace sf;

#include <map>
#include <string>
using namespace std;

class CConsole
{
public:
    typedef sf::String (*MFP)(sf::String value);    //function pointer type

    void bindFunction(string name, MFP func);    //binds a function
    void unbindFunction(string name);    //unbinds desired function
private:
    map <string, MFP> functions;
}

コンソールにバインドしようとしている関数がグローバル名前空間のものである限り、これはすべてうまくいきます。しかし、これはうまくいきません。コンソールにバインドしたいすべてのネストされた関数に対して常にグローバル ラッパー関数を作成するのは非効率的です。

「MFP」がすべての名前空間の関数ポインタを受け入れるようにすることはまったく可能ですか? たとえば、次のコードを完全に機能させるには?

#include "console.h"    //code shown above

//Let's also pretend CConsole has an sf::String(sf::String value) method called consoleFunc that returns "Hello from the CConsole namespace!"

sf::String globalFunc(sf::String value)
{
     return "Hello from the global namespace!";
}

int main()
{
    CConsole console;
    console->bindFunction("global", globalFunc);
    console->bindFunction("CConsole", CConsole::consoleFunc);
    return 0;
}
4

1 に答える 1

0

あなたの例では、bindFunction任意の非メンバー関数、または任意のクラスの静的メンバー関数を呼び出すことができます。bindFunction非静的メンバーは型が異なるため、使用できません。

于 2013-05-16T11:21:47.333 に答える