gpt4 book ai didi

c++ - 重写虚函数仅通过调用约定有什么不同意味着什么?

转载 作者:可可西里 更新时间:2023-11-01 16:28:45 27 4
gpt4 key购买 nike

我正在尝试实现 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;
}
};

最佳答案

为每个方法添加 STDMETHODCALLTYPE

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

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

关于c++ - 重写虚函数仅通过调用约定有什么不同意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16719714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com