Member function pointer to a member function
Trying to create a class that has a member function pointer that points to a member function.
Here is a basic example of what I'm trying to do:
class X
{
public:
X();
short myFunction(void*, DWORD, byte);
short (&X::fptr)(void*, DWORD, byte);
};
X::X()
{
fptr = &X::myFunction;
}
in the main program
X myx;
char *a;
DWORD b;
byte c;
myx.*fptr(a, b, c); -> on compile I get 'fptr': identifier not found
I have tried using typedef in various ways and have had no success. Any help would be awesome!
Re: Member function pointer to a member function
Quote:
Originally Posted by
joshuawoodz
Trying to create a class that has a member function pointer that points to a member function.
Here is a basic example of what I'm trying to do:
Usage of typedef makes this a lot simpler:
Code:
class X
{
public:
typedef short (X::*ptrFn)(void*, DWORD, byte);
X();
short myFunction(void*, DWORD, byte);
ptrFn fptr;
};
So now there is a member variable of type "ptrFn" called fptr.
In main()
Code:
int main()
{
X myx;
char *a;
DWORD b;
byte c;
X::ptrFn fn = myx.fptr;
(myx.*fn)(a, b, c);
}
This now compiles. Note how the expression is broken up in main() to see how this all works.
Now, you can do a substitution:
Code:
int main()
{
X myx;
char *a;
DWORD b;
byte c;
(myx.*(myx.fptr))(a, b, c);
}
This now compiles. All I did was substitute back into the expression the declaration of "fn".
Regards,
Paul McKenzie
Re: Member function pointer to a member function