471,585 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

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

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
2 1883
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
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.

Similar topics

8 posts views Thread by Bryan Parkoff | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.