[comp.lang.c++] Calling member funktions via pointers?

jan@nasobem.stgt.sub.org (Jan Schiefer) (06/29/91)

Hello!

I am stuck with a (simple?) problem: I want to call a member function of
a class A via a pointer. I have typedef'd a pointer to a member function
(which takes no arguments and gives no return value) of class A like this:

typedef void (A::*PMF_A)(void);

I keep some of those PMF_A's (Pointer to Member Function of A) in an array
(called CommandTable), which is a static member of A and consists of structs
of the form

struct ACommand { unsigned Element; PMF_A fp; };

, fp being the pointer to Element's associated function.

I select one of those structs in another member function of A and call the
function from there with

(this->*CommandTable[i].fp)();

where i is the index into the CommandTable.

And this is what happens:
I end up at the right member function, fine. But: the 'this' pointer has a
different value there. Which causes all the data this member function
manipulates, to be different. Garbage, that is.

In other words:
When I call a member function of a given class indirectly, how do I ensure
that the 'this' pointer is correct?

I am using Borland C++ 2.0 on an AT class machine.

(And I promise that if some net.guru helps me solve that problem, I'll rush
out on monday and buy a copy of the ARM (so that I won't have to go to the
bookstore every time and read it there :-))

Thanks,

Jan

--
    Jan Schiefer, DL5UE, Degerlocherstr. 5, 7000 Stuttgart 70, Germany
                       jan@nasobem.stgt.sub.org
   "The bus transceiver controls, DALI and DALO, are used to control the
      bus transceivers."   - AM7990 LANCE technical reference manual