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;
}
相关推荐
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
C++多重继承引发的重复调用问题与解决方法
主要介绍了C++多重继承引发的重复调用问题与解决方法,结合具体实例形式分析了C++多重调用中的重复调用问题及相应的解决方法,需要的朋友可以参考下
C++习题 多重继承
c++多重继承的一个习题
C++多重继承与虚继承分析
本文以实例形式较为全面的讲述了C++的多重继承与虚继承,是大家深入学习C++面向对象程序设计所必须要掌握的知识点,具体内容如下: 一、多重继承 我们知道,在单继承中,派生类的对象中包含了基类部分 和 派生类自定义部分。同样的,在多重继承(multiple inheritance)关系中,派生类的对象包含了每个基类的子对象和自定义成员的子对象。下面是一个多重继承关系图: class A{ /* */ }; class B{ /* */ }; class C : public A { /* */ }; class D : public B, public C { /* */ }; C继承了
C++多重继承
c++ 多重继承 结构体 自己写的保证运行


