from base class calling methods defined in derived classes through pointer to member functions

L

Luca

Hi, I have a quite complex question to ask you:

I have defined a base class where I would like to have a map holding
pointers to member functions defined in derived classes.

To be more precise I would like my base class to have the following
member:

map<string, pointer_to_member_function> myClassMap;

my base class also has a string member - say m_sValue - whose value
can be the same of one of the string values contained in the map
My base class should also have a public method, say "execute" that
does the following:

1.retrieve the map pair whose key has the same value as m_sValue
2.dereference the poniter to member function contained in the second
member of the map pair element, thus invoking the member function
defined in the derived class

Is that possible to do?

Thank you

Luca
 
C

Christoph Rabel

Luca said:
Hi, I have a quite complex question to ask you:

map<string, pointer_to_member_function> myClassMap;

1.retrieve the map pair whose key has the same value as m_sValue
2.dereference the poniter to member function contained in the second
member of the map pair element, thus invoking the member function
defined in the derived class

Is that possible to do?

The problem is that you cant store memberfunctions of different classes
in a map. Base::bar() and Derived::bar() have different types.

Maybe as a workaround, you could do it this way:
(Untested, just to show the idea...)

class Base
{
public:
typedef std::map<std::string, void (*)(Base*)> FunctionMap;
string funcname;
FunctionMap functions;

void execute()
{
FunctionMap::iterator it = functions.find(funcname);
if (it != functions.end())
(it->second)(this);
}
};

class Derived : public Base
{
void doSomething() {};

public:
static void execute(Base* base)
{
static_cast<Derived*>(base)->doSomething();
}

Derived() { functions["Derived"] = &execute }
};

hth

Christoph
 
R

Rob Williscroft

Luca wrote in
Hi, I have a quite complex question to ask you:

I have defined a base class where I would like to have a map holding
pointers to member functions defined in derived classes.

To be more precise I would like my base class to have the following
member:

map<string, pointer_to_member_function> myClassMap;

struct derived;

typedef void (derived::*pointer_to_member_function)();
my base class also has a string member - say m_sValue - whose value
can be the same of one of the string values contained in the map
My base class should also have a public method, say "execute" that
does the following:

1.retrieve the map pair whose key has the same value as m_sValue

1a. ? don't you want to check there is such a pair in the map ?

2.dereference the poniter to member function contained in the second
member of the map pair element,
(this->*(ptr->second))();

thus invoking the member function
defined in the derived class

Is that possible to do?

Yes.

Rob.
 

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,012
Latest member
RoxanneDzm

Latest Threads

Top