编译器警告(等级 1)C4946

reinterpret_cast 在相关类之间使用:“class1”和“class2”

不要使用 reinterpret_cast 在相关类型之间进行转换。 为多态类型使用 static_cast ,或者,使用 dynamic_cast

默认情况下,关闭此警告。 有关更多信息,请参见默认情况下处于关闭状态的编译器警告

下面的代码示例生成 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);
}