在C ++中键入检查 [英] Type checking in C++
问题描述
在C ++中,我想知道对象的实际类型是来自同一个类,而不是相同的类或派生的类。这类似于以下C#代码:
类库
{
}
Class Child:Base
{
}
基本childObject = new Child();
If(childObject.GetType()== typeof(Child))
{
//做一些代码
}
谢谢!
解决方案方法,你可以做到这一点。首先,您可以使用
typeid
运算符,它返回一个包含对象类型信息的type_info
结构。例如:Base * ptr = / * ... * /
if(typeid(* ptr)= = typeid(DerivedType)){
/ * ... ptr指向DerivedType ... * /
}
注意,你必须使用
typeid(* ptr)
,而不是typeid(ptr)
这里。如果你使用typeid(ptr)
,你会得到type_info
Base * ,因为指针具有类型Base *
,无论它指向什么。
需要注意的一点是,这将检查
ptr
是否正确为DerivedType
。如果ptr
指向从DerivedType
派生的类型的对象(也许EvenMoreDerivedType
),此代码将无法正常工作。
另一种方法是检查是否指向某种类型的对象robust是使用
dynamic_cast
运算符。dynamic_cast
在运行时执行检查的类型转换,如果转换成功则产生有效的指针,否则为NULL。例如:Base * ptr = / * ... * /;
DerivedType * derived = dynamic_cast< DerivedType *>(ptr);
if(derived){
/ * ...指向DerivedType ... * /
}
这有额外的好处,如果
ptr
指向类似EvenMoreDerivedType
,因为EvenMoreDerivedType
继承自DerivedType
。
作为最后的想法,你有时会看到这样的代码:
Base * ptr = / * ... * /
if(DerivedType * derived = dynamic_cast< DerivedType *>(ptr)){
/ * ...指向DerivedType ... * /
}
这在本地将
派生
指针指向if
语句,并使用非零值在C ++中求值为true
的事实。我个人认为这更容易阅读,更不容易出错,但一切都是以最简单的方式。
希望这有助于!
In C++, I want to know whether the actual type of the object is from the same class, not the same class or a derived one. This is similar to the following C# code:
Class Base { } Class Child:Base { } Base childObject = new Child(); If (childObject.GetType() == typeof(Child)) { // do some code }
Thanks!
解决方案There are two ways that you can do this. First, you can use the
typeid
operator, which returns atype_info
structure containing information about the type of the object. For example:Base* ptr = /* ... */ if (typeid(*ptr) == typeid(DerivedType)) { /* ... ptr points to a DerivedType ... */ }
Notice that you have to use
typeid(*ptr)
and nottypeid(ptr)
here. If you usetypeid(ptr)
, then you'll get back atype_info
object forBase*
, since the pointer has typeBase*
regardless of what it points at.An important point to note is that this will check if what
ptr
points at is exactly asDerivedType
. Ifptr
is pointing at an object of a type derived fromDerivedType
(maybe anEvenMoreDerivedType
), this code will not work correctly.An alternative way of checking whether you are pointing at an object of some type that is a bit more robust is to use the
dynamic_cast
operator.dynamic_cast
performs a checked typecast at runtime that will yield a valid pointer if the cast succeeds and NULL otherwise. For example:Base* ptr = /* ... */; DerivedType* derived = dynamic_cast<DerivedType*>(ptr); if (derived) { /* ... points to a DerivedType ... */ }
This has the added advantage that if
ptr
points at something like anEvenMoreDerivedType
, the cast will still succeed becauseEvenMoreDerivedType
inherits fromDerivedType
.As a final thought, you sometimes see code like this:
Base* ptr = /* ... */ if (DerivedType* derived = dynamic_cast<DerivedType*>(ptr)) { /* ... points to a DerivedType ... */ }
This locally-scopes the
derived
pointer to the body of theif
statement and uses the fact that nonzero values evaluate totrue
in C++. I personally find this easier to read and less error-prone, but by all means go with what's easiest for you.Hope this helps!
这篇关于在C ++中键入检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!