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

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

问题描述

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

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() - 因为变量是 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.

示例代码(我现在意识到朋友在这里并不是真的必要):

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 变量,并返回一个引用。 处理器:: 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天全站免登陆