C++ 中的静态与动态类型检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1347691/
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 vs dynamic type checking in C++
提问by rkb
I want to know what are static and dynamic type checking and the differences between them.
我想知道什么是静态和动态类型检查以及它们之间的区别。
回答by VoidPointer
Static type checking means that type checking occurs at compile time. No type information is used at runtime in that case.
静态类型检查意味着在编译时进行类型检查。在这种情况下,在运行时不使用任何类型信息。
Dynamic type checking occurs when type information is used at runtime. C++ uses a mechanism called RTTI (runtime type information) to implement this. The most common example where RTTI is used is the dynamic_cast operator which allows downcasting of polymorphic types:
在运行时使用类型信息时会发生动态类型检查。C++ 使用一种称为 RTTI(运行时类型信息)的机制来实现这一点。使用 RTTI 的最常见示例是 dynamic_cast 运算符,它允许向下转换多态类型:
// assuming that Circle derives from Shape...
Shape *shape = new Circle(50);
Circle *circle = dynamic_cast<Circle*> shape;
Furthermore, you can use the typeid operator to find out about the runtime type of objects. For example, you can use it to check whether the shape in the example is a circle or a rectangle. Here is some further information.
此外,您可以使用 typeid 运算符来了解对象的运行时类型。例如,您可以使用它来检查示例中的形状是圆形还是矩形。这里有一些进一步的信息。
回答by yesraaj
C++ have very little support for dynamic type checking one is through dynamic_castand other is through type id.Both can be used only when RTTI support is enabled in compiler.
C++ 对动态类型检查的支持很少,一种是通过dynamic_cast,另一种是通过type id 。两者都只能在编译器中启用 RTTI 支持时使用。
TYPE& dynamic_cast<TYPE&> (object);
TYPE* dynamic_cast<TYPE*> (object);
The dynamic_cast keyword casts a datum from one pointer or reference type to another, performing a runtime check to ensure the validity of the cast.
dynamic_cast 关键字将数据从一个指针或引用类型转换为另一种,执行运行时检查以确保转换的有效性。
If you attempt to cast to pointer to a type that is not a type of actual object, the result of the cast will be NULL. If you attempt to cast to reference to a type that is not a type of actual object, the cast will throw a bad_cast exception.
如果您尝试强制转换为指向不是实际对象类型的类型的指针,则强制转换的结果将为 NULL。如果您尝试强制转换为对不是实际对象类型的类型的引用,则该类型转换将抛出 bad_cast 异常。
Make sure there is atleast one virtual function in Base class to make dynamicast work.
确保 Base 类中至少有一个虚函数来使 dynamicast 工作。
// expre_typeid_Operator.cpp
// compile with: /GR /EHsc
#include <iostream>
#include <typeinfo.h>
class Base {
public:
virtual void vvfunc() {}
};
class Derived : public Base {};
using namespace std;
int main() {
Derived* pd = new Derived;
Base* pb = pd;
cout << typeid( pb ).name() << endl; //prints "class Base *"
cout << typeid( *pb ).name() << endl; //prints "class Derived"
cout << typeid( pd ).name() << endl; //prints "class Derived *"
cout << typeid( *pd ).name() << endl; //prints "class Derived"
delete pd;
}
回答by user9876
Assume you have:
假设你有:
class A {};
class B:A {};
A* a = new B();
B* b = new B();
For the static type, you look at how the variable is declared.
对于静态类型,您查看变量是如何声明的。
A* a = ...
B* b = ...
So the static type of a
is A*
(or to put it another way, the static type of *a
is A
).
所以a
is的静态类型A*
(或者换句话说,*a
is的静态类型A
)。
And the static type of b
is B*
(or to put it another way, the static type of *b
is B
).
而b
is的静态类型B*
(或者换句话说,*b
is的静态类型B
)。
Note that a
and b
have a static type that is fixed by it's declaration - it doesn't matter what you put in them, they will keep the same static type. ("static" means "unchanging").
请注意,a
并且b
有一个由它的声明固定的静态类型 - 无论您在其中放入什么,它们都将保持相同的静态类型。(“静态”意味着“不变”)。
For the dynamic type, you look at what happens to be in the variable right now.
对于动态类型,您现在查看变量中的内容。
a = new B();
b = new B();
So the dynamic types of a
and b
are both B*
(or to put it another way, the dynamic types of *a
and *b
are both B
).
所以a
and的动态类型b
都是B*
(或者换句话说,*a
and的动态类型*b
都是B
)。
Note that the dynamic type can change - if you did a = new A()
then they dynamic type of a
just changed to A*
. Sometimes you don't know what the dynamic type is - e.g. if you do a = somefunc()
then a
might have dynamic type A*
, B*
or even C*
(if some code you haven't seen defines C
as a subclass of A
or B
).
请注意,动态类型可以更改 - 如果您这样做了,a = new A()
那么它们的动态类型a
刚刚更改为A*
. 有时你不知道动态类型是什么——例如,如果你知道,a = somefunc()
那么a
可能有动态类型A*
,B*
或者甚至C*
(如果你没有看到一些代码定义C
为A
or的子类B
)。
If A
had a virtual
method on it, then you could use dynamic_cast
to figure out what the dynamic type is. (Typically, if you're using this sort of code, you want to be able to do delete a
; for that to work A
's destructor has to be virtual
. And making A
's destructor virtual is enough for dynamic_cast
to work).
如果上面A
有一个virtual
方法,那么您可以使用它dynamic_cast
来确定动态类型是什么。(通常,如果您正在使用此类代码,您希望能够做到delete a
;要使其工作A
,析构函数必须是virtual
。使A
的析构函数为虚拟就足以dynamic_cast
工作)。
回答by Reed Copsey
There are multiple types of casts available in C++.
C++ 中有多种类型的强制转换。
The most common would be to use static_castin order to cast a variable from one type of pointer to another. However, you can also use dynamic_cast, which will check to make sure (at runtime) that the pointers are of the correct type. With dynamic_cast, if the pointer is not of the right type, at runtime, it will return 0 instead.
最常见的是使用static_cast将变量从一种类型的指针转换为另一种类型。但是,您也可以使用dynamic_cast,它会检查以确保(在运行时)指针的类型正确。使用 dynamic_cast,如果指针的类型不正确,在运行时,它将返回 0。
// Assume these classes exist
// class A
// class B
// class C : B
C* c = new C();
A* a = new A();
B* b = static_cast<B*>(a); // this will work!
b = dynamic_cast<B*>(a); // b == NULL
b = dynamic_cast<B*>(c); // b is valid
回答by Daniel Bingham
Static type checking is type checking that is done at compile time. This is the only type of type checking that C++ does. Dynamic type checking is type checking done at run time. This is usually seen in dynamic interpreted languages, but is less common in compiled languages. Last I checked, C++ doesn't do any sort of dynamic type checking.
静态类型检查是在编译时完成的类型检查。这是 C++ 所做的唯一类型检查。动态类型检查是在运行时完成的类型检查。这通常出现在动态解释语言中,但在编译语言中不太常见。最后我检查,C++ 不做任何类型的动态类型检查。
Edit: Apparently I'm out of date. See Reed's comment below.
编辑:显然我已经过时了。请参阅下面里德的评论。