在地图中存储指向成员函数的指针 [英] Store pointers to member function in the map

查看:169
本文介绍了在地图中存储指向成员函数的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串映射到实例成员函数,并将每个映射存储在映射中。

I'd like to map string to an instance member functions, and store each mapping in the map.

这样做的干净的方法是什么?

What is the clean way of doing something like that?

class  MyClass
{
   //........
   virtual double GetX();
   virtual double GetSomethingElse();
   virtual double GetT();
   virtual double GetRR();
   //........
};


class Processor
{
 private:
      typedef double (MyClass::*MemFuncGetter)();
      static map<std::string, MemFuncGetter> descrToFuncMap;

 public:
        static void Initialize();
        void Process(Myclass m, string);
};

void Processor::Initialize()
{

     descrToFuncMap["X"]=&MyClass::GetX;
     descrToFuncMap["SomethingElse"]=&MyClass::GetSomethingElse;
     descrToFuncMap["RR"]=&MyClass::GetRR;
     descrToFuncMap["T"]=&MyClass::GetT;
};
void Processor::Process(MyClass ms, const std::string& key)
{
     map<std::string, Getter>::iterator found=descrToFuncMap.find(key);
     if(found!=descrToFuncMap.end())
     {
        MemFuncGetter memFunc=found->second;
        double dResult=(ms).*memFunc();    
        std::cout<<"Command="<<key<<", and result="<<result<<std::end;      
      }
 }  

让我知道如果您看到此方法的问题

let me know if you see a problem with this approach and what are common idioms for that?

也许,我应该使用if-else-if语句链,因为我有有限数量的成员函数,而不是一个令人困惑的func指针映射

BTW,我在 c ++ - faq-lite

推荐答案

对我来说很好,但是因为 descrToFuncMap 需要声明 static 如果你打算从静态函数 Initialize()中初始化它。

Looks fine to me, but for the fact that descrToFuncMap needs to be declared static if you intend to initialise it from inside the static function Initialize().

如果你想确认 Initialize()被调用,并且只被调用一次,你可以使用Singleton模式。基本上,如果你没有做多线程,这意味着在它自己的类中调用 descrToFuncMap (称为 FuncMap )与一个私人构造函数调用 Initialize()。然后你添加一个 static 类型 FuncMap 的局部变量到 Processor :: Process / code> - 因为变量是 static ,它仍然存在,只被初始化一次。

If you want to make sure that Initialize() gets called, and gets called just once, you can use the Singleton pattern. Basically, if you aren't doing multithreading, that just means wrapping descrToFuncMap inside its own class (called say FuncMap) with a private constructor that calls Initialize(). Then you add a static local variable of type FuncMap to Processor::Process() -- because the variable is static, it persists and is only initialised once.

示例代码(我现在意识到 friend 在这里不是必需的):

Example code (I now realise that friend isn't really necessary here):

class Processor {
private:
    typedef double (MyClass::*MemFuncGetter)();

    class FuncMap {
    public:
        FuncMap() {
            descrToFuncMap["X"]=&MyClass::GetX;
            descrToFuncMap["SomethingElse"]=&MyClass::GetSomethingElse;
            descrToFuncMap["RR"]=&MyClass::GetRR;
            descrToFuncMap["T"]=&MyClass::GetT;
        }

        // Of course you could encapsulate this, but its hardly worth
        // the bother since the whole class is private anyway.
        map<std::string, MemFuncGetter> descrToFuncMap;
    };

public:
    void Process(Myclass m, string);
};

void Processor::Process(MyClass ms, const std::string& key) {
    static FuncMap fm;      // Only gets initialised on first call
    map<std::string, Getter>::iterator found=fm.descrToFuncMap.find(key);
    if(found!=fm.descrToFuncMap.end()) {
        MemFuncGetter memFunc=found->second;
        double dResult=(ms).*memFunc();    
        std::cout<<"Command="<<key<<", and result="<<result<<std::end;      
    }
}

这不是真正的Singleton模式函数可以创建自己的 FuncMap 的单独实例,但它足以满足您的需要。对于trueSingleton,您可以声明 FuncMap 的构造函数private并添加一个静态方法,例如 getInstance() ,它将唯一的实例定义为 static 变量,并返回一个引用。 Processor :: Process()会使用

This is not the "true" Singleton pattern as different functions could create their own, separate instances of FuncMap, but it's enough for what you need. For "true" Singleton, you would declare FuncMap's constructor private and add a static method, say getInstance(), which defined the one-and-only instance as a static variable and returned a reference to that. Processor::Process() would then use this with

FuncMap& fm = FuncMap::getInstance();

这篇关于在地图中存储指向成员函数的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆