Feb6

【转载】c++中typeid与type_info

Author: leeon  Click: 7487   Date: 2014.02.06 @ 11:45:49 am Category: c/c++

     在学习java中的Class类,有getClass方法,其特性与c++中的typeid很类似,java中调用getClass方法和c++中的typeid运算符等价。

     typeid是C++的一个关键字,其作用是在程序运行时,返回对象的类型,即所谓的RTTI(Run Time Type Identification), 它使程序能够获取由指针或引用所指向的对象的实际类型,即允许“用指向基类的指针或引用来操作对象”的程序能够获取到“这些指针或引用所指对象”的实际派生类型。在C++中,为了支持RTTI提供了两个操作符:dynamic_cast和typeid。

typeid操作符的返回结果是名为type_info的标准库类型的对象的引用。 type_info在typeinfo头文件中定义:


class type_info {public:
  virtual ~type_info();
  bool operator== (const type_info& rhs) const;
  bool operator!= (const type_info& rhs) const;
  bool before (const type_info& rhs) const;
  const char* name() const;private:
  type_info (const type_info& rhs);
  type_info& operator= (const type_info& rhs);
}; 

如下内容摘自http://www.cplusplus.com/reference/typeinfo/type_info/:


typeid can be applied to any typed expression, including a type itself, to retrieve the its type_info.

When typeid is applied to a reference or dereferenced pointer to an object of a polymorphic class type (a class declaring or inheriting a virtual function), it considers its dynamic type (i.e., the type of the most derived object). This requires the RTTI (Run-time type information) to be available.

When typeid is applied to a dereferenced null pointer a bad_typeid exception is thrown.


type_info成员介绍如下:


operator==
operator!=
Comparison operators. They return whether the two types describe the same type.
A derived type is not considered the same type as any of its base classes.
before
Returns true if the type precedes the type of rhs in the collation order.
The collation order is just an internal order kept by a particular implementation and is not necessarily related to inheritance relations or declaring order.
name
Returns a null-terminated character sequence with a human-readable name for the type.
copy constructor and copy operator
These are private, preventing type_info values from being copiable.

示例代码:


// type_info example
#include <iostream>
#include <typeinfo>
using namespace std;

struct Base {};
struct Derived : Base {};
struct Poly_Base {virtual void Member(){}};
struct Poly_Derived: Poly_Base {};

int main() {
// built-in types:
int i;
int * pi;
cout << "int is: " << typeid(int).name() << endl;
cout << " i is: " << typeid(i).name() << endl;
cout << " pi is: " << typeid(pi).name() << endl;
cout << "*pi is: " << typeid(*pi).name() << endl << endl;

// non-polymorphic types:
Derived derived;
Base* pbase = &derived;
cout << "derived is: " << typeid(derived).name() << endl;
cout << " *pbase is: " << typeid(*pbase).name() << endl;
cout << boolalpha << "same type? ";
cout << ( typeid(derived)==typeid(*pbase) ) << endl << endl;

// polymorphic types:
Poly_Derived polyderived;
Poly_Base* ppolybase = &polyderived;
cout << "polyderived is: " << typeid(polyderived).name() << endl;
cout << " *ppolybase is: " << typeid(*ppolybase).name() << endl;
cout << boolalpha << "same type? ";
cout << ( typeid(polyderived)==typeid(*ppolybase) ) << endl << endl;
}

输出:


int is: int
i is: int
pi is: int *
*pi is: int

derived is: struct Derived
*pbase is: struct Base
same type? false

polyderived is: struct Poly_Derived
*ppolybase is: struct Poly_Derived
same type? true



TAG:   typeid cpp type_info c++

    评论
    • 提交

    分类

    标签

    归档

    最新评论

    Abyss在00:04:28评论了
    Linux中ramdisk,tmpfs,ramfs的介绍与性能测试
    shallwe99在10:21:17评论了
    【原创】如何在微信小程序开发中正确的使用vant ui组件
    默一在09:04:53评论了
    Berkeley DB 由浅入深【转自架构师杨建】
    Memory在14:09:22评论了
    【原创】最佳PHP框架选择(phalcon,yaf,laravel,thinkphp,yii)
    leo在17:57:04评论了
    shell中使用while循环ssh的注意事项

    我看过的书

    链接

    其他

    访问本站种子 本站平均热度:8823 c° 本站链接数:1 个 本站标签数:464 个 本站被评论次数:94 次