Topic: Tricky stuff with ptrs to member functions


Author: skp@bnr.co.uk (Steve Perryman)
Date: Fri, 19 Aug 1994 12:31:07 GMT
Raw View
I have some (possibly) devious code code involving ptrs to member functions
I cannot get to work correctly. Here it is :


typedef int (A::*Op)(void) ;


class A
{
    protected:

    virtual int opA1(void) ;
} ;

class B : public A
{
    protected:

    virtual int opB1(void) ;
    virtual int opB2(void) ;
} ;

class C : public virtual B
{
    protected:

    virtual int opA1(void) ;
    virtual int opB2(void) ;
} ;


I want to set up a table of ptr to member ops thus :

Op ops[] ;

So for class C I have : Op c_ops[] = { (Op) &opA1, (Op) &opB1, (Op) &opB2 } ;

Some code that does : C* obj ; ... ; (obj->*(c_ops[i] ) )(999) ;
C::opB2 is not called and the table points to something in A, which is wrong.

If I try : Op my_op = (Op) &C::opB2 ; (obj->*my_op)(999) ; ... Core dump !!!

I'd be grateful if someone could tell me (if it's possible) how to get the
desired behaviour. It seems to work when C inherits non-virtual but there is
multiple inheritance below C that will not subsequently compile. My feeling
is that the virtual base class with virtuals causes the problem, or the table
entries being cast to Op (which is upwards) is the culprit. The virtuals for
C have been correctly resolved by the time the danger code is called (as I
have checked) .


Thanks in advance,
Steven Perryman
skp@bnr.co.uk