Share via


編譯器警告 (層級 1) C4946

在關聯的類別之間使用的 reinterpret_cast:'class1' 和 'class2'

請勿使用 reinterpret_cast 在相關類型之間轉換。 請改用 static_cast ,或針對多型類型使用 dynamic_cast

根據預設,此警告為關閉。 如需詳細資訊,請參閱 Compiler Warnings That Are Off by Default

下列程式碼範例會產生 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);
}