靜態施法與動態施法[重復]

[英]static cast versus dynamic cast [duplicate]


Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast

可能重復:常規強制轉換與static_cast對比dynamic_cast

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

我不太習慣何時使用靜態演員和動態。有什么解釋嗎?

7 个解决方案

#1


31  

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.

如果不需要額外檢查,請使用static_cast。正如Arkaitz所說,由於dynamic_cast執行額外檢查,因此它需要RTTI信息,因此具有更大的運行時開銷,而static_cast則在編譯時執行。

#2


7  

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.

對於編譯時檢查,請使用static_cast(限於編譯器知道的內容)。對於運行時檢查,請使用dynamic_cast(僅限於帶有RTTI的類)。如果沒有檢查,請使用reinterpret_cast。

#3


3  

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

dynamic_cast檢查運行時可用的信息,例如RTTI,它還遍歷類層次結構以查看是否可以進行此類轉換。

Check C++ type casting tutorial at cplusplus.com

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

#4


2  

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.

動態強制轉換需要RTTI並且與靜態強制轉換相比具有一定的魔力static_cast只是一個編譯時間轉換,檢查是否可以通過一些簡單的規則將原始類提升為轉換類作為繼承。

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

例如,在虛擬繼承的情況下,只有dynamic_cast可以解決這種情況。

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

另外,如果無法進行強制轉換,dynamic_cast將返回NULL,因此您可以做出不同的決定。

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.

另一方面,dynamic_cast較慢,因為它意味着正在執行某些代碼,並且如前所述,它需要啟用RTTI,這會增加二進制文件的大小。

#5


2  

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.

動態轉換在運行時完成,因此需要運行時類型信息。當您不確定自己的類型時,可以執行此操作:強制轉換可能會失敗,返回值為null。它也只能用於指針和引用。

#6


2  

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.

static_cast類似於舊的C樣式轉換,幾乎可以應用於任何東西。當您確定某些類型時,將使用static_cast。例如,我在int和enum之間進行轉換時通常使用static_cast。

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 ++語言教程:類型轉換

#7


0  

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會拋出錯誤:)希望這會有所幫助! :)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/08/10/7254160dc147a6c7676545ff1dcb6f07.html



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