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值 |
#include <iostream>
using namespace std;
using namespace std;
int main()
{
int a = 4;
{
int a = 4;
int stop_flag;
cout << typeid(a).name() << endl;
cout << typeid(a).name() << endl;
cout <<
typeid(4).name() << endl;
cin >> stop_flag;
}
cin >> stop_flag;
}
浙公网安备 33010602011771号