8

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

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

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

如果您发现这种方法有问题,请告诉我,常见的成语是什么?

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

顺便说一句,我在c++-faq-lite中找到了一些有用的信息

4

3 回答 3

6

对我来说看起来不错,但是如果您打算从静态函数内部对其进行初始化,则descrToFuncMap需要声明这一事实。staticInitialize()

如果你想确保它Initialize()被调用,并且只被调用一次,你可以使用单例模式。基本上,如果你不做多线程,那只是意味着descrToFuncMapFuncMap一个调用Initialize(). 然后,您将static类型的局部变量添加FuncMapProcessor::Process()-- 因为该变量是static,所以它会持续存在并且只初始化一次。

示例代码(我现在意识到这friend并不是真正需要的):

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()然后将其与

FuncMap& fm = FuncMap::getInstance();
于 2009-03-10T16:08:58.690 回答
0

我会改变

void Processor::Process(MyClass ms, std::string key)

void Processor::Process(const MyClass& ms, const std::string& key)

暂时看不到任何不良的副作用。可能使用 boost::function 作为映射值,将来会更容易。

于 2009-03-10T16:03:47.657 回答
0

如果您使用函数指针映射,请避免使用“虚拟”。在这种情况下,使用 'virtual' 关键字不会有太大帮助。例如

descrToFuncMap["X"]=&MyClass::GetX;

即使GetX被 MyClass 的派生类覆盖,也将始终调用“ MyClass::GetX ”函数。

通常你不会在类中有大量函数,而不是使用 map 你可以创建简单的结构数组并使用 for 循环。如果函数的数量很少,map和array的性能差异不会很大。类似于下面代码的东西会起作用

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

typedef double (MyClass::*MemFuncGetter)();

struct FuncTable
{
    const char* m_pFuncName;
    MemFuncGetter m_pFuncPtr;
};

class Processor
{          
 public:
        void Process(Myclass& m, string);
};

static FuncTable descrToFuncMap[]
{
    { "X",  &MyClass::GetX},
    { "SomethingElse", &MyClass::GetSomethingElse },
    { "RR", &MyClass::GetRR},
    { "T", &MyClass::GetT}
};

void Processor::Process(MyClass& ms, const std::string& key)
{
    int functablesize = sizeof(descrToFuncMap)/sizeof(descrToFuncMap[0])

    for(int i=0; i< functablesize; ++i)
    {   
        if( strcmp(key.c_str(), descrToFuncMap[i].m_pFuncName)==0)
        {
            MemFuncGetter memFunc=descrToFuncMap[i].m_pFuncPtr;
            double dResult=(ms).*memFunc();    
            std::cout<<"Command="<<key<<"result="<<result<<std::end;
            break;
        }
    }     
 }
于 2009-03-11T12:23:58.033 回答