Compartilhar via


Compiler Warning (level 1) C4946

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

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

By default, this warning is off. For more information, see Compiler Warnings That Are Off by Default.

The following code example 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);
}