[英]C++ - Pointer to a class method
I have to set up a pointer to a library function ( IHTMLDocument2::write
) which is a method of the class IHTMLDocument2
. 我必须设置一个指向库函数( IHTMLDocument2::write
)的指针,该函数是类IHTMLDocument2
的方法。 (for the curious: i have to hook that function with Detours) (出于好奇:我必须将该功能与Detours挂钩)
I can't do this directly, because of type mismatch, neither can I use a cast ( reinterpret_cast<>
which is the "right one" afaik doesn't work) 由于类型不匹配,我不能直接执行此操作,也不能使用强制类型转换( reinterpret_cast<>
,这是“正确的” afaik无法正常工作)
Here's what I am doing: 这是我在做什么:
HRESULT (WINAPI *Real_IHTMLDocument2_write)(SAFEARRAY *) = &IHTMLDocument2::write
Thanks for your help! 谢谢你的帮助!
The pointer to function has the following type: 函数的指针具有以下类型:
HRESULT (WINAPI IHTMLDocument2::*)(SAFEARRAY*)
As you can see, it's qualified with it's class name. 如您所见,它的类名是合格的。 It requires an instance of a class to call on (because it is not a static function): 它需要一个类的实例来调用(因为它不是静态函数):
typedef HRESULT (WINAPI IHTMLDocument2::*DocumentWriter)(SAFEARRAY*);
DocumentWriter writeFunction = &IHTMLDocument2::write;
IHTMLDocument2 someDocument = /* Get an instance */;
IHTMLDocument2 *someDocumentPointer = /* Get an instance */;
(someDocument.*writefunction)(/* blah */);
(someDocumentPointer->*writefunction)(/* blah */);
You need to use a member function pointer . 您需要使用成员函数指针 。 A normal function pointer won't work, because when you call a (non-static) class member function there is an implicit this
pointer referring to an instance of the class. 普通的函数指针将不起作用,因为当您调用(非静态)类成员函数时,存在一个隐式的this
指针, this
指针指向类的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.