如何从静态函数调用非静态成员函数? [英] how to call non static member function from Static Function?

查看:255
本文介绍了如何从静态函数调用非静态成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有

我非法调用非静态成员函数

i have got illegal call of non-static member function

如何从静态调用非静态成员函数功能?是可能的。

how to call non static member function from Static Function? is that possible.

class MyClass
{
public:	
	MyClass();
	();
	Cmd_MouseWheel(short zDelta, POINT pt);
private:
	LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);
	static void MouseHook(int nCode, WPARAM wParam, LPARAM lParam);
	HHOOK hMouseHook;
};

MyClass::MyClass()
{
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, &MouseHookProc, NULL, 0);
}
MyClass::~MyClass()
{
   if (hMouseHook)
	UnhookWindowsHookEx(hMouseHook);
}

LRESULT CALLBACK MyClass::MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	MouseHook(nCode, wParam, lParam);
	return CallNextHookEx(0, nCode, wParam, lParam);
}

MyClass::Cmd_MouseWheel(short zDelta, POINT pt)
{
	// code here use all non static function;
}

void MyClass::MouseHook(int nCode, WPARAM wParam, LPARAM lParam)
{
MSLLHOOKSTRUCT * pMouseStruct = (MSLLHOOKSTRUCT *)lParam; 
if (nCode == 0) 
    { 
        switch (wParam) 
	{
	case WM_MOUSEWHEEL:
	Cmd_MouseWheel( GET_WHEEL_DELTA_WPARAM(wParam), pMouseStruct->pt); // illegal call of non-static member function
				break;
	}
     }
}


感谢您提前帮助

Thanks for Help in Advance

推荐答案

我会通过使用一个静态挂钩来解决这个问题,但也有一个静态的对象实例列表,它们是钩子的观察者。  然后,当您收到鼠标滚轮消息时,通过对观察者列表中的每个对象实例执行以下操作,将其广播到所有正在侦听的对象实例:

I would fix this by having a single static hook, but also having a static list of object instances that are observers of the hook.  Then when you get a mouse wheel message, broadcast it to all object instances that are listening by doing something like:

对于观察者列表中的每个对象实例,告诉关于鼠标事件的对象实例。

让构造函数和析构函数通过添加自身或将自身移除到静态观察者列表来订阅和取消订阅观察。

Let the constructor and destructor subscribe and unsubscribe from observations by adding itself or removing itself to the static list of observers.

小心以线程安全的方式使用观察者列表。 (即,在添加,删除和迭代时)

Be careful to use the observer list in a thread-safe way. (i.e., while adding, removing, and iterating)


这篇关于如何从静态函数调用非静态成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆