【发布时间】:2011-01-03 21:48:06
【问题描述】:
基本上,我有一个纯虚拟类 Base,以及一个继承自 Base 的具体类 Derived。然后我分配一块内存并通过简单的转换将其视为 Derived 数组。然后,我使用 = 填充数组。最后,我循环遍历数组,尝试调用在 Base 中声明并在 Derived 中定义的虚方法 GetIndex。
问题是我最终得到一个访问冲突异常,试图读取指向 Base 的 vtable 的指针(在 Visual Studio 调试中,这显示为 __vfptr,它始终为 0xbaadf00d)。
以下是我遇到的问题的一个简单示例:
#include "stdafx.h"
#include "windows.h"
struct Base
{
virtual int GetIndex() const = 0;
};
struct Derived : public Base
{
int index;
Derived()
{
static int test = 0;
index = test++;
}
int GetIndex() const
{
return index;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int count = 4;
// Also fails with malloc
Derived* pDerived = (Derived*)HeapAlloc(GetProcessHeap(), 0, sizeof(Derived) * count);
for (int i = 0; i < count; i++)
{
Derived t;
pDerived[i] = t;
}
// Should print 0 1 2 3
for (int i = 0; i < count; i++)
{
Base& lc = pDerived[i];
printf("%d\n", lc.GetIndex()); // FAIL!
}
return 0;
}
这种行为只发生在通过 HeapAlloc 或 malloc 分配内存时;如果使用 new[],它工作正常。 (另外,cstor之前被调用了4次,所以输出是4 5 6 7。)
【问题讨论】:
-
作为参考,这是通过替换解决的:Derived t; pDerived[i] = t; with: new (&pDerived[i]) Derived();
标签: c++ inheritance dynamic-memory-allocation vtable