What does overriding virtual function differs only by calling convention mean?(重写虚函数的区别仅在于调用约定意味着什么?)
问题描述
我正在尝试实现 未知.我按照指示到发球台,但它不起作用.当我尝试编译时,我得到:
I am trying to implement IUnknown. I followed the instruction to the tee but it isn't working. When I try to compile I get:
Error 2 error C2695: 'testInterfaceImplementation::AddRef': overriding virtual function differs from 'IUnknown::AddRef' only by calling convention c:usersseanmdesktop est estsource.cpp 6 1 test
Error 3 error C2695: 'testInterfaceImplementation::QueryInterface': overriding virtual function differs from 'IUnknown::QueryInterface' only by calling convention c:usersseanmdesktop est estsource.cpp 14 1 test
Error 4 error C2695: 'testInterfaceImplementation::Release': overriding virtual function differs from 'IUnknown::Release' only by calling convention c:usersseanmdesktop est estsource.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
The base class(IUnknown
) methods are declared as STDMETHODCALLTYPE
(which is a macro for __stdcall
). When you override a virtual method, it has to have the same calling convention as the original which in this case is __stdcall
这篇关于重写虚函数的区别仅在于调用约定意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:重写虚函数的区别仅在于调用约定意味着什么?


基础教程推荐
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01