Pointer to non-static member functions

1
2
3
4
5
6
7
8
9
10
// Notes: 	I never allocate this class on stack
//		NetworkMessage::getOpcodes() returns a DWORD between 0x0 - 0xFFFFFF
class Protocol{
private:
	int (*m_HandlerMap[0x1000000])(NetworkMessage &msg);
	int m_LoginHandler(NetworkMessage &msg);
public:
	void RegisterHandlers(){ m_HandlerMap[0] = &m_LoginHandler; return;}
	int ParseMessage(NetworkMessage &msg){ if(m_HandlerMap[msg.getOpcodes()] != NULL) return m_HandlerMap[msg.getOpcodes()](msg);}
};


My problem is that unless I make m_LoginHandler static I get compile errors. Is it possible to create a pointer to a non-static member function from within the other class functions?
Yes it's possible, but you can't use pointer to non-static member function without an object instance.

edit:

for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct A
{
	void f() { std::cout << "func f() from A"; }
};

struct B
{
	void f(A& object, void (A::*func_ptr)()) { (object.*func_ptr)(); }
};

int main()
{
	A a;
	B b;
	b.f(a, &A::f);
	return 0;
}
Last edited on
I see thanks for the example.
Topic archived. No new replies allowed.