共用方式為


編譯器警告 (層級 1) C4436

從虛擬基底「到「class2 " class1 "的 dynamic_cast 在建構函式 (Constructor) 或解構函式可能無法以部分建構的物件使用/vd2 編譯或定義"使用 #pragma vtordisp (2) 的 class2 實際

編譯器遇到具有下列特性的 dynamic_cast 作業。

  • 這個型別是從基底類別指標至衍生類別的指標。

  • 衍生類別 (Derived Class) 實際上會繼承基底類別。

  • 衍生類別不具備虛擬基底的 vtordisp 欄位。

  • 這個型別在衍生類別中的建構函式或解構函式,或從衍生類別 (Derived Class) 進一步繼承的類別中。

警告表示 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;
}

請參閱

參考

dynamic_cast 運算子

vtordisp

編譯器警告 (層級 4) C4437