I have this setup:
I have a class, and one of the members is a function pointer. I want to be able to implement various possible versions of the function it points to in the same class the function pointer resides in. However, I get various errors when I try to initialize the function pointer to a function that also is a member of the same class.
I've shown the first part, then the error it generates, then in the second part I try to remedy it by declaring the function pointer to be a member of the Vec class, but it still doesn't work (I show the error codes of visual C++ 6). Of course, I couldn't then put this same function pointer in the baseclass, but it could at least point to a member function (sadly I can't even get this much to work properly).
Code:
#include <iostream>
using namespace std;
//pFunc is of data type function pointer which returns void, accepts void as formal parameter
typedef void(*pFunc)(void);
void SomeFunc()
{
cout << "This is just some func" << endl;
}
typedef struct
{
Vec()
{
x = y = z = w = 0;
funcPtr = 0;
}
void VectorFunc()
{
cout << "This is the vector func" << endl;
}
pFunc funcPtr;
float x;
float y;
float z;
float w; //complex plane
}Vec,*VectorPTR;
typedef void(Vec::*VecFuncPtr)(void);
int main(void)
{
Vec a;
a.funcPtr = SomeFunc; //works
a.funcPtr();
a.funcPtr = Vec::VectorFunc; //error:
/*
error C2440: '=' : cannot convert from 'void (__thiscall Vec::*)(void)' to 'void (__cdecl *)(void)'
*/
VecFuncPtr b;
b = Vec::VectorFunc;
b(); //error:
/*
error C2064: term does not evaluate to a function
*/
return 0;
}