#include <typeinfo>
#include <iostream>
using namespace std;
struct A
{
int a;
int b;
};
void main()
{
A b;
b.a = 3;
b.b = 4;
int a = 4;
cout << typeid(a).name() << endl;
cout << typeid(b).name() << endl;
}
下文转载自:http://blog.programfan.com/article.asp?id=45931
http://www.cppblog.com/smagle/archive/2010/05/14/115286.html
typeid操作符
typeid表达式形如:
typeid(expr);
这里expr是任意表达式或者类型名。如果表达式的类型是类类型且至少包含有一个虚函数,则typeid操作符返回表达式的动态类型,需要在运行时计算;否则,typeid操作符返回表达式的静态类型,在编译时就可以计算。
typeid操作符的返回结果是名为type_info的标准库类型的对象的引用(在头文件typeinfo中定义)。标准并没有确切定义type_info,它的确切定义编译器相关的,但是标准却规定了其实现必需提供如下四种操作:
t1 == t2 | 如果两个对象t1和t2类型相同,则返回true;否则返回false |
t1 != t2 | 如果两个对象t1和t2类型不同,则返回true;否则返回false |
t.name() | 返回类型的C-style字符串,类型名字用系统相关的方法产生 |
t1.before(t2) | 返回指出t1是否出现在t2之前的bool值 |
type_info类提供了public虚析构函数,以使用户能够用其作为基类。它的默认构造函数和拷贝构造函数及赋值操作符都定义为private,所以不能定义或复制type_info类型的对象。程序中创建type_info对象的唯一方法是使用typeid操作符(由此可见,如果把typeid看作函数的话,其应该是type_info的友元)。type_info的name成员函数返回C-style的字符串,用来表示相应的类型名,但务必注意这个返回的类型名与程序中使用的相应类型名并不一定一致(往往如此,见后面的程序),这是由实现所决定的,标准只要求实现为每个类型返回唯一的字符串。例如:
#include<iostream> using namespacestd; class Base {}; class Derived:public Base {}; int main() { cout <<typeid(int).name()<< endl <<typeid(unsigned).name()<< endl <<typeid(long).name()<< endl <<typeid(unsignedlong).name()<< endl <<typeid(char).name()<< endl <<typeid(unsignedchar).name()<< endl <<typeid(float).name()<< endl <<typeid(double).name()<< endl <<typeid(string).name()<< endl <<typeid(Base).name()<< endl <<typeid(Derived).name()<< endl <<typeid(type_info).name()<< endl; return 0; } |
在MinGW2.05下的运行结果: i j l m c h f d Ss 4Base 7Derived St9type_info Terminated with return code 0 Press any key to continue ... |
注意:当把typeid作用于指针的解引用*p时,若指针p为0,则:如果p指向的类型是带虚函数的类类型,则typeid(*p)在运行时抛出一个bad_typeid异常;否则,typeid(*p)的结果与p的值是不相关的,在编译时就可以确定。typeid表达式的这点性质与sizeof表达式相似但又有区别,sizeof一定是在编译时进行计算,也就是说,其只考虑表达式的静态类型,与表达式的动态类型无关(即使有虚函数存在)。
参考文献:
[1] C++ Primer(Edition 4)
[2] Thinking in C++(Edition 2)
[3] International Standard:ISO/IEC 14882:1998