Store pointers to member function in the map.

S

sasha

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 problems with this approach and what are
common idioms for that?

Perhaps, I should use if-else-if statement chain, given that I have a
limited number of member functions, instead of a confusing map of func
pointers

BTW, I found some of the useful info here in the c++-faq-lite
 
R

red floyd

sasha said:
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 problems with this approach and what are
common idioms for that?

The map is OK, but I don't think you're going to get the virtual
behavior you want.

Better might be:

class MyClass
{
private:
//........
virtual double do_GetX();
virtual double do_GetSomethingElse();
virtual double do_GetT();
virtual double do_GetRR();

public:
// note that these functions are non-virtual
double GetX(){ return do_GetX(); }
double GetSomethingElse() { return do_GetSomethingElse(); }
double GetT(){ return do_GetT(); }
double GetRR(){ return do_GetRR(); }
//........
};
 
S

sasha

sasha said:
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  problems with this approach and what are
common idioms for that?

The map is OK, but I don't think you're going to get the virtual
behavior you want.


Please explain what prevents the virtual behavior?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top