C++ 静态转换与动态转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1254777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
static cast versus dynamic cast
提问by rayimag
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?
我不太清楚什么时候使用静态转换,什么时候使用动态转换。请问有什么解释吗?
回答by Nick Meyer
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
在编译时执行。
回答by xan
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。
回答by Diaa Sami
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,它还遍历类层次结构以查看此类转换是否可行。
回答by Jesse Vogt
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
当您有问题的某些类型时将使用。例如,我通常static_cast
在int
和之间转换时使用 a 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
回答by xtofl
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。它也只能用于指针和引用。
回答by Arkaitz Jimenez
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,这会增加二进制文件的大小。
回答by faya
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 会抛出错误:) 希望这会有所帮助!:)