代码之家  ›  专栏  ›  技术社区  ›  zakk

类方法的指针

  •  0
  • zakk  · 技术社区  · 16 年前

    我必须设置指向库函数的指针( IHTMLDocument2::write )这是一个类的方法 IHTMLDocument2 . (对于好奇的人来说:我不得不绕道而行)

    我不能直接这样做,因为类型不匹配,我也不能使用一个演员。 reinterpret_cast<> 哪一个是“正确的”afaik不起作用)

    我正在做的是:

    HRESULT (WINAPI *Real_IHTMLDocument2_write)(SAFEARRAY *) = &IHTMLDocument2::write
    

    谢谢你的帮助!

    2 回复  |  直到 16 年前
        1
  •  6
  •   GManNickG    16 年前

    指向函数的指针具有以下类型:

    HRESULT (WINAPI IHTMLDocument2::*)(SAFEARRAY*)
    

    正如你所看到的,它的名字是合格的。它需要调用类的实例(因为它不是静态函数):

    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 */);
    
        2
  •  4
  •   Charles Salvia    16 年前

    你需要使用 member function pointer . 普通函数指针不起作用,因为当调用(非静态)类成员函数时 this 指向类实例的指针。