从虚拟基“到“class2 "的class1的dynamic_cast在构造函数或析构函数可能会与部分构造的对象使用/vd2编译或定义“使用#pragma vtordisp (2)的class2 "实际
编译器遇到具有以下特征的 dynamic_cast 操作。
该转换是从基类指针到派生类指针。
该派生类虚拟继承基类。
该派生类没有虚foundation一个 vtordisp 字段。
如果是在该派生类的构造函数或析构函数,或者从派生类进一步继承的一些选件类中找到。
警告意味着 dynamic_cast 可能无法正确执行,因此,如果它在一部分构造的对象运行。如果派生的构造函数/析构函数在某些其他派生的对象,子对象操作该操作。如果在警告名为的该派生类从不是进行进一步派生,该警告可以忽略。
示例
下面的示例生成C4436并演示从缺少 vtordisp 字段显示的代码生成问题。
// C4436.cpp
// To see the warning and runtime assert, compile with: /W1
// To eliminate the warning and assert, compile with: /W1 /vd2
// or compile with: /W1 /DFIX
#include <cassert>
struct A
{
public:
virtual ~A() {}
};
#if defined(FIX)
#pragma vtordisp(push, 2)
#endif
struct B : virtual A
{
B()
{
A* a = static_cast<A*>(this);
B* b = dynamic_cast<B*>(a); // C4436
assert(this == b); // assert unless compiled with /vd2
}
};
#if defined(FIX)
#pragma vtordisp(pop)
#endif
struct C : B
{
int i;
};
int main()
{
C c;
}