cast conversion for member pointer

The return type for the pointer to member function is void. I want to reassign to the function pointer a new function within the SomeClass class with a different type (USHORT).

How do I do this? I realize there may be altogether better ways to write it, but...

I tried dynamic cast...but I didn't work so well.

want to point to &SomeClass::anotherfx instead....of ptr_fx = & SomeClass::somefx

Thank you in advance.
T

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
typedef unsigned short int USHORT;

class SomeClass
{
public:
	virtual void somefx(USHORT a, USHORT b) { cout<<"a * b = "<<a*b<<"\n"; };
	USHORT anotherfx(USHORT a){ cout<<"test\n";};
...


void (SomeClass::*fx_ptr) (USHORT, USHORT)

...

int _tmain(int argc, _TCHAR* argv[])
{
	//declare pointer to class fx
	void (SomeClass::*fx_ptr) (USHORT, USHORT);
	fx_ptr = &SomeClass::somefx;
	SomeClass * class_ptr = new SomeClass;
	(class_ptr->*fx_ptr)(3,4);
	//let's declare pointer to class fx with a typedef instead
	typedef void(SomeClass::*VPF)(USHORT, USHORT); // this can go above main under class dec if wants
	//let's set this function pointer to address of the function itself
	VPF ptr_fx = &SomeClass::somefx;
	//guess what - below - we can still use the original object we created with the new pointer
	(class_ptr->*ptr_fx)(5,5);
	system("PAUSE");
	// reassign pointer to a fx of diff type
	ptr_fx = &SomeClass::anotherfx; //ERROR
You can't. That's like saying "Why can't I change my class that contains an int into a class that contains a string?" They are completely different types.
Topic archived. No new replies allowed.