覆盖虚函数的不同之处仅在于调用约定意味着什么?

时间:2013-05-23 16:56:52

标签: c++ com

我正在尝试实施IUnknown。我按照指示进入发球台,但它不起作用。当我尝试编译时,我得到:

Error   2   error C2695: 'testInterfaceImplementation::AddRef': overriding virtual function differs from 'IUnknown::AddRef' only by calling convention  c:\users\seanm\desktop\test\test\source.cpp 6   1   test
Error   3   error C2695: 'testInterfaceImplementation::QueryInterface': overriding virtual function differs from 'IUnknown::QueryInterface' only by calling convention  c:\users\seanm\desktop\test\test\source.cpp 14  1   test
Error   4   error C2695: 'testInterfaceImplementation::Release': overriding virtual function differs from 'IUnknown::Release' only by calling convention    c:\users\seanm\desktop\test\test\source.cpp 22  1   test

来自此代码:

#include <Windows.h>
#include <tchar.h>

class testInterfaceImplementation : public IUnknown {
    protected:
        ULONG AddRef() 
        {
            MessageBox(NULL,
                _T("TEST1"),
                _T("TEST1"),
                NULL);
            return 0;
        }
        HRESULT QueryInterface(IN REFIID riid, OUT void **ppvObject) 
        {
            MessageBox(NULL,
                _T("TEST2"),
                _T("TEST2"),
                NULL);
            return S_OK;
        }
        ULONG Release() {
            MessageBox(NULL,
                _T("TEST3"),
                _T("TEST3"),
                NULL);
            return 0;
        }
};

1 个答案:

答案 0 :(得分:18)

为每种方法添加STDMETHODCALLTYPE

ULONG STDMETHODCALLTYPE AddRef() 
HRESULT STDMETHODCALLTYPE QueryInterface(IN REFIID riid, OUT void **ppvObject) 
ULONG STDMETHODCALLTYPE Release() 

基类(IUnknown)方法声明为STDMETHODCALLTYPE(这是__stdcall的宏)。覆盖虚方法时,它必须与原始方法具有相同的调用约定,在本例中为__stdcall