By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Luca wrote:
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
Jul 22 '05 #2

P: n/a
Luca wrote in news:a9**************************@posting.google.c om:
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 ?

map<string, pointer_to_member_function>::iterator ptr;
ptr = myClassMap.find( myClassMap );
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.