472,954 Members | 2,067 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 1942
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
8
by: LAvoisieR | last post by:
Following test code behaviour suprised me and I dont know what's wrong with this. I have two overloaded constructors within base class and virtual destructor implemented. Derived class uses...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.