[英]static cast versus dynamic cast [duplicate]

Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast


I don't quite get when to use static cast and when dynamic. Any explanation please?


7 个解决方案



Use dynamic_cast when casting from a base class type to a derived class type. It checks that the object being cast is actually of the derived class type and returns a null pointer if the object is not of the desired type (unless you're casting to a reference type -- then it throws a bad_cast exception).

從基類類型轉換為派生類類型時使用dynamic_cast。它檢查正在轉換的對象實際上是派生類類型,如果對象不是所需類型,則返回空指針(除非您正在轉換為引用類型 - 然后它會拋出bad_cast異常)。

Use static_cast if this extra check is not necessary. As Arkaitz said, since dynamic_cast performs the extra check, it requires RTTI information and thus has a greater runtime overhead, whereas static_cast is performed at compile-time.




In some contexts, like this one, "static" refers to compile-time and "dynamic" refers to run-time.


For compile-time checking, use static_cast (limited to what the compiler knows). For run-time checking, use dynamic_cast (limited to classes with RTTI). For no checking, use reinterpret_cast.




dynamic_cast checks information available at run-time, such as RTTI, it also traverses class hierarchies to see if such cast is possible.


Check C++ type casting tutorial at cplusplus.com

在cplusplus.com上查看C ++類型轉換教程



Dynamic cast requires RTTI and does some magic compared to static cast. static_cast is just a compile time cast, checks if origin class can be promoted to the casted class by some simple rules as inheritance.


For example, in cases of virtual inheritance only dynamic_cast can resolve the situation.


Apart, dynamic_cast will return NULL if the cast is not possible, so you can take a different decision.


In the other hand, dynamic_cast is slower, as it implies some code being executed, and as said previously, it needs RTTI enabled which increases the size of the binaries.




Static casting is done by the compiler: it treats the result as the target type, no matter what. You do this when you're absolutely sure about the argument being of the target type.


Dynamic casting is done at runtime, and thus requires runtime type information. You do this when you're unsure about the type you have: the cast may fail, which is shown by the return value being null. It can also only be done for pointers and references.




static_cast is similar to the old C style cast and can be applied to just about anything. static_cast would be used when you certain of the types in question. For example, I usually use a static_cast when casting between int and enum.


dynamic_cast can only be used with pointers and references. On failure to cast, a null pointer is returned. dynamic_cast is generally used when resolving pointers to classes used in inheritance where you want to make sure the pointer you are casting is of the expected type.

dynamic_cast只能與指針和引用一起使用。如果無法強制轉換,則返回空指針。 dynamic_cast通常在解析指向繼承中使用的類的指針時使用,您希望確保所投射的指針屬於預期類型。

Also check out C++ : Documentation : C++ Language Tutorial : Type Casting

另請參閱C ++:文檔:C ++語言教程:類型轉換



If you are talking about C++. Then static_cast is not safe type of casting. It can cast to your type but if its wrong it will not throw any error/message. So you will get bad object from that. And the dynamic_cast is throwing error if the casting failed :) Hope this helps ! :)

如果你在談論C ++。那么static_cast不是安全的類型。它可以轉換為您的類型,但如果它錯了它不會拋出任何錯誤/消息。所以你會從那里得到不好的對象。如果轉換失敗,dynamic_cast會拋出錯誤:)希望這會有所幫助! :)



粤ICP备14056181号  © 2014-2021 ITdaan.com