Partager via


Compiler Warning (level 1) C4946 

Error Message

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

reinterpret_cast should not be used to cast between related types. Use either static_cast or a C-style cast.

This warning is off by default. 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);
}