在C ++中键入检查 [英] Type checking in C++

查看:91
本文介绍了在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 a type_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 not typeid(ptr) here. If you use typeid(ptr), then you'll get back a type_info object for Base*, since the pointer has type Base* regardless of what it points at.

An important point to note is that this will check if what ptr points at is exactly as DerivedType. If ptr is pointing at an object of a type derived from DerivedType (maybe an EvenMoreDerivedType), 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 an EvenMoreDerivedType, the cast will still succeed because EvenMoreDerivedType inherits from DerivedType.

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 the if statement and uses the fact that nonzero values evaluate to true 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆