I have problem with the pointer to member function call. The address of pointer "this" outside the function pointer call is different than inside the call thus all access to the class variables results wrong values.
I include the code here.
class ClassInterface
{
public:
ClassInterface(void);
~ClassInterface(void);
};
class ClassA:public ClassInterface
{
public:
float _a;
public:
ClassA(void);
~ClassA(void);
virtual void Update();
};
class ClassB:public ClassA
{
public:
ClassB(void);
~ClassB(void);
void Update();
void ProcessTaskB(void*);
};
//ClassB.CPP
void ClassB::ProcessTaskB(void*)
{
printf("ClassB::ProcessTaskB\n");
printf("Address of myB INSIDE callback = %d\n",this);
_a += 100;
}
//test CPP
#include "stdafx.h"
#include "ClassInterface.h"
#include "ClassA.h"
#include "ClassB.h"
typedef void (ClassInterface::*Callback) (void* );
int _tmain(int argc, _TCHAR* argv[])
{
ClassA* myA = new ClassA();
ClassB* myB = new ClassB();
Callback fptrB = (Callback) &(ClassB::ProcessTaskB);
printf("Address of myB outside callback = %d\n",myB);
(myB->*fptrB)(NULL);
return 0;
}
And this is the output:
Address of myB OUTSIDE callback = 1332696
Address of myB INSIDE callback = 1332700
Thus the statement _a += 100; does not make change to _a. It made change to address (&_a + 4).
I have no clue to resolve this. Please help me fix.