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

问题描述

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

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 指针映射

顺便说一句,我在 中找到了一些有用的信息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() 被调用,并且只被调用一次,您可以使用单例模式.基本上,如果你不做多线程,那只是意味着用一个调用 Initialize() 的私有构造函数将 descrToFuncMap 包装在它自己的类(称为 say FuncMap)中.然后将 FuncMap 类型的 static 局部变量添加到 Processor::Process() -- 因为该变量是 staticcode>,它会持续存在并且只初始化一次.

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;
    }
}

这不是真正的"单例模式,因为不同的函数可以创建它们自己的、单独的 FuncMap 实例,但这足以满足您的需要.对于真正的"单例,您可以将 FuncMap 的构造函数声明为私有并添加一个静态方法,例如 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();

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

07-11 05:31