Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Will member function pointers work for virtuals?

1 view
Skip to first unread message

srp113

unread,
Aug 28, 2009, 2:09:37 PM8/28/09
to
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

red floyd

unread,
Aug 28, 2009, 2:26:23 PM8/28/09
to
On Aug 28, 11:09 am, srp113 <sunilsreenivas2...@yahoo.com> wrote:
> 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;

srp113

unread,
Aug 28, 2009, 3:52:51 PM8/28/09
to
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

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

Pete Becker

unread,
Aug 28, 2009, 4:27:50 PM8/28/09
to
srp113 wrote:
> Hi Red,
> Is there an issue with pointers to virtual functions in the
> mechanism I described before (not allowed by standard/wont work)?

struct Base
{
virtual void f();
};

struct Derived : Base
{
void f();
};

void test(Base *bp)
{
void (Base::*pmf)() = &Base::f;
(bp->*pmf)();
}

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.

> 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).
>

If you're concerned about speed, measure. Calling a virtual function
through a pointer to member function can be quite complicated, depending
on the class hierarchy.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)

srp113

unread,
Aug 28, 2009, 6:18:16 PM8/28/09
to
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

Juha Nieminen

unread,
Aug 29, 2009, 7:09:54 AM8/29/09
to
srp113 wrote:
> 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.

Pete Becker

unread,
Aug 29, 2009, 8:01:39 AM8/29/09
to
Juha Nieminen wrote:
>
>> 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.

Calling a function (virtual or otherwise) through a pointer to member
function is not the same as calling a function by name through a pointer
or reference to an object. It can be much more complicated: the runtime
code has to match the type of the object with the class of the
pointed-to member function and sort out any necessary adjustments.

0 new messages