C++抽象基类多重继承问题

Intel0011 2017-11-14 07:09:40
已发C++区,这里也放一份,求解,C++真是怪异,还是喜欢纯C
#define  INITGUID
#include <Windows.h> // Define HRESULT
#include <iostream>
#include <objbase.h> // Define interface
#include <iomanip>
using namespace std;

interface IDictionary : public IUnknown
{
virtual void _stdcall Initialize() = 0;
virtual void _stdcall Uninitialize() = 0;
};

interface ISpellCheck : public IUnknown
{
virtual void _stdcall CheckWord() = 0;
};

class CDictionary : public IDictionary, public ISpellCheck
{
public:
virtual HRESULT _stdcall QueryInterface(const IID&, void** ppv)
{
static int i;
cout << "QueryInterface " << i++ << endl;
return 0;
};
virtual ULONG _stdcall AddRef() { cout << "AddRef" << endl; return 0; };
virtual ULONG _stdcall Release() { cout << "Release" << endl; return 0; };

virtual void _stdcall Initialize() { cout << "Initialize" << endl; };
virtual void _stdcall Uninitialize() { cout << "Uninitialize" << endl; };

virtual void _stdcall CheckWord() { cout << "CheckWord" << endl; };
int a;
};

// {3340DDDC-490B-4a3c-9CC9-0E3DB217D36A}
DEFINE_GUID(MyIID, 0x3340dddc, 0x490b, 0x4a3c, 0x9c, 0xc9, 0xe, 0x3d, 0xb2, 0x17, 0xd3, 0x6a);


int main(void)
{
CDictionary *pA = new CDictionary;

cout << "对象地址:" << (int*)pA << endl;
cout << "第1个ppv地址:" << (int*)pA << endl;
cout << "第1个ppv地址内容:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *(int*)pA << endl;

cout << "第2个ppv地址:" << (int*)pA+1 << endl;
cout << "第2个ppv地址内容:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)pA+1) << endl;

cout << "虚函数表 — 第1个函数指针:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << ((int*)*(int*)pA+0) << endl;

cout << "虚函数表 — 第1个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+0) << endl;
cout << "虚函数表 — 第2个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+1) << endl; // 为啥地址和下面的不一样
cout << "虚函数表 — 第3个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+2) << endl;
cout << "虚函数表 — 第4个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+3) << endl;
cout << "虚函数表 — 第5个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+4) << endl;
cout << "虚函数表 — 第6个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+5) << endl;

cout << "虚函数表 — 第2个函数指针:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << ((int*)*((int*)pA+1)) << endl;

cout << "虚函数表 — 第1个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+0) << endl; // 为啥地址和上面的不一样
cout << "虚函数表 — 第2个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+1) << endl;
cout << "虚函数表 — 第3个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+2) << endl;
cout << "虚函数表 — 第4个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+3) << endl;
cout << "虚函数表 — 第5个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+4) << endl;

cout << "数据成员地址: " << &pA->a << endl;

IDictionary *e = pA;
ISpellCheck *f = pA;

e->QueryInterface(MyIID, NULL); // 验证是否是调用相同函数
f->QueryInterface(MyIID, NULL);

delete pA;

return 0;
}
...全文
597 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Intel0011 2017-11-14
  • 打赏
  • 举报
回复
我的输出结果为

对象地址:00831DC0
第1个ppv地址:00831DC0
第1个ppv地址内容:004712CC
第2个ppv地址:00831DC4
第2个ppv地址内容:004712B8
虚函数表 — 第1个函数指针:004712CC
虚函数表 — 第1个函数地址:0040125D
虚函数表 — 第2个函数地址:00401078
虚函数表 — 第3个函数地址:004012DA
虚函数表 — 第4个函数地址:004012C6
虚函数表 — 第5个函数地址:0040105F
虚函数表 — 第6个函数地址:00000000
虚函数表 — 第2个函数指针:004712B8
虚函数表 — 第1个函数地址:0040109B
虚函数表 — 第2个函数地址:00401087
虚函数表 — 第3个函数地址:00401096
虚函数表 — 第4个函数地址:00401091
虚函数表 — 第5个函数地址:00000000
数据成员地址: 00831DC8
QueryInterface 0
QueryInterface 1
Press any key to continue

21,458

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧