Export (0) Print
Expand All
Expand Minimize

Compiler Warning (level 1) C4436

dynamic_cast from virtual base 'class1' to 'class2' in constructor or destructor could fail with partially-constructed object Compile with /vd2 or define 'class2' with #pragma vtordisp(2) in effect

The compiler has encountered a dynamic_cast operation with the following characteristics.

  • The cast is from a base class pointer to a derived class pointer.

  • The derived class virtually inherits the base class.

  • The derived class does not have a vtordisp field for the virtual base.

  • The cast is found in a constructor or destructor of the derived class, or some class which further inherits from the derived class.

The warning indicates the dynamic_cast might not perform correctly, if it is operating on a partially-constructed object. That happens if the derived constructor/destructor is operating on a sub-object of some further derived object. If the derived class named in the warning is never further derived, the warning can be ignored.

The following sample generates C4436 and demonstrates the code generation issue that arises from the missing vtordisp field.

// 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
    virtual ~A() {}

#if defined(FIX)
#pragma vtordisp(push, 2)
struct B : virtual A
        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)

struct C : B
    int i;

int main()
    C c;
© 2014 Microsoft