C++抽象基类多重继承时,成员函数地址不一致问题(详见程序)

Intel0011 Intel0011 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#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;
}
...全文

相关推荐

C++多重继承二义性原理实例解析

在派生类中对基类成员访问应该是唯一的,但是在多继承时,可能会导致对基类某成员访问出现不一致的情况,这就是C++多继承中的二义性。 有两种继承的情况会产生多义性 一、如果一个派生类从多个基类派生,而这些基类又有一个共同的基类,则在对该基类中声明的成员变量进行访问时,可能产生二义性,继承关系如下图所示: #include <iostream> using namespace std; class A{ public: int a; }; class B1 : public A{ public: int b1; }; class B2 : public A{ public: int

weixin_38687218

C++多重继承引发的重复调用问题与解决方法

主要介绍了C++多重继承引发的重复调用问题与解决方法,结合具体实例形式分析了C++多重调用中的重复调用问题及相应的解决方法,需要的朋友可以参考下

weixin_38684509

C++习题 多重继承

c++多重继承的一个习题

Cube_007

C++多重继承与虚继承分析

本文以实例形式较为全面的讲述了C++的多重继承与虚继承,是大家深入学习C++面向对象程序设计所必须要掌握的知识点,具体内容如下: 一、多重继承 我们知道,在单继承中,派生类的对象中包含了基类部分 和 派生类自定义部分。同样的,在多重继承(multiple inheritance)关系中,派生类的对象包含了每个基类的子对象和自定义成员的子对象。下面是一个多重继承关系图: class A{ /* */ }; class B{ /* */ }; class C : public A { /* */ }; class D : public B, public C { /* */ }; C继承了

weixin_38557980

C++多重继承

c++ 多重继承 结构体 自己写的保证运行

良先生vip
评论 7
收藏 已收藏
分享
返回顶部
Intel0011
C++ 语言 65,214 成员 250,514 内容