Will member function pointers work for virtuals?

S

srp113

Hi,
I have a base class (State) that has a list of virtual/non-virtual
functions that implement processing logic for different kinds of
events that can happen in a system (signature for all of these
functions is same). I want to build a jump table (lets say a simple
array) that maps an event(integer) to a member function pointer:
jumptable[eventid]->fptr(eventdata).
I have classes responding to these events derived from base "State"
class (the hierarchy can have depth of > 2 i.e. State1 derives from
Base, State2 derives from State1...) and these virutal methods could
get overriden in different "state" classes to provide different
behavior for a given event. (state1/state2 both can have different
processing logic for event1 but same for event2) . My questions:
a)Will jumptable[eventid]->fptr(eventdata) work as expected? ( esp for
virtual functions will this call the most derived classes
implementation?),
b)Is there a performance penalty with this implementation (going
through 2 lookups one for jump table and then for vtable)?
c)If answer to b) is yes, are there any alternatives?
The reason for jump table is that I want to reuse this implementation
of state pattern in multiple applications
Thanks Much,
Sunil
 
R

red floyd

Hi,
   I have a base class (State) that has a list of virtual/non-virtual
functions that implement  processing logic for different kinds of
events that can happen in a system (signature for all of these
functions is same). I want to build a jump table (lets say a simple
array) that maps an event(integer) to a member function pointer:
jumptable[eventid]->fptr(eventdata).
I have classes responding to these events derived from base "State"
class (the hierarchy can have depth of  > 2  i.e. State1 derives from
Base, State2 derives from State1...) and these virutal methods could
get overriden in different "state" classes to provide different
behavior for a given event. (state1/state2 both can have different
processing logic for event1 but same for event2) . My questions:
a)Will jumptable[eventid]->fptr(eventdata) work as expected? ( esp for
virtual functions will this call the most derived classes
implementation?),

A better method that's a bit more clear, and doesn't rely on pointer
to virtual members
is to use a public interface to private virtual (similar to the
Template design pattern).

e.g.:

class C {
private:
virtual void vf();
public:
void f();

// remainder redacted
};

void C::f()
{
vf();
}


void (C::*pf)() = &C::f;
 
S

srp113

Hi Red,
Is there an issue with pointers to virtual functions in the
mechanism I described before (not allowed by standard/wont work)?
With the implementation you have given, there is increased overhead,
if I understand correctly my jumptable under your suggestion will be
setup to non-virtual functions, which in turn will call virtual
functions, which in turn will cause vtable lookup (3 levels of
indirections).
Thanks Much,
Sunil


Hi,
   I have a base class (State) that has a list of virtual/non-virtual
functions that implement  processing logic for different kinds of
events that can happen in a system (signature for all of these
functions is same). I want to build a jump table (lets say a simple
array) that maps an event(integer) to a member function pointer:
jumptable[eventid]->fptr(eventdata).
I have classes responding to these events derived from base "State"
class (the hierarchy can have depth of  > 2  i.e. State1 derives from
Base, State2 derives from State1...) and these virutal methods could
get overriden in different "state" classes to provide different
behavior for a given event. (state1/state2 both can have different
processing logic for event1 but same for event2) . My questions:
a)Will jumptable[eventid]->fptr(eventdata) work as expected? ( esp for
virtual functions will this call the most derived classes
implementation?),

A better method that's a bit more clear, and doesn't rely on pointer
to virtual members
is to use a public interface to private virtual (similar to the
Template design pattern).

e.g.:

class C {
   private:
       virtual void vf();
   public:
       void f();

// remainder redacted

};

void C::f()
{
    vf();

}

void (C::*pf)() = &C::f;- Hide quoted text -

- Show quoted text -
 
S

srp113

Hey Pete,
Thanks for your response.
When you call test with a pointer to a Base object it calls Base::f.
When you call test with a pointer to a Derivd object it calls
Derived::f. Try it.
I did try it and it worked on my machine. I wanted to be sure its OK
to do this as per C++ standard.
If you're concerned about speed, measure. Calling avirtualfunction
through a pointer tomemberfunctioncan be quite complicated, depending
on the class hierarchy.
I have used a tool called quantify from rational and that does report
increased # cpu cycles if I call virtual functions through member
ptrs vs calling them directly. I ran a test that measured for 10,000
function calls and that showed a difference of almost 4,10,000 CPU
cycles almost 3 millisecs on a 143 MHZ processor. Not sure though if
this will be real problem in my application.. that seems to be hard
call to make
Thanks
 
J

Juha Nieminen

srp113 said:
a)Will jumptable[eventid]->fptr(eventdata) work as expected? ( esp for
virtual functions will this call the most derived classes
implementation?),

Yes, member function pointers fully take into account dynamic binding
and will call the proper function. (AFAIK that's the reason why member
function pointers usually have double the size of a regular function
pointer.)
b)Is there a performance penalty with this implementation (going
through 2 lookups one for jump table and then for vtable)?

Virtual function calls always have a small penalty compared to regular
function calls. In most cases it's so small that it doesn't matter in
practice.
 

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,776
Messages
2,569,603
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top