Export (0) Print
Expand All
Expand Minimize

Compiler Warning (level 1) C4946

reinterpret_cast used between related classes: 'class1' and 'class2'

reinterpret_cast should not be used to cast between related types. Use static_cast instead, or for polymorphic types, use dynamic_cast.

By default this warning is off. See Compiler Warnings That Are Off by Default for more information.

The following sample generates C4946:

// C4946.cpp
// compile with: /W1
#pragma warning (default : 4946)
class a {
public:
   a() : m(0) {}
   int m;
};

class b : public virtual a {
};

class b2 : public virtual a {
};

class c : public b, public b2 {
};

int main() {
   c* pC = new c;
   a* pA = reinterpret_cast<a*>(pC);   // C4946
   // try the following line instead
   // a* pA = static_cast<a*>(pC);
}

Community Additions

ADD
Show:
© 2014 Microsoft