typeid
来源:互联网 发布:邮箱数据都有什么内容 编辑:程序博客网 时间:2024/05/17 09:17
转自:百度百科http://baike.baidu.com/view/1375171.htm
目录
- C++的typeid
- dede typeid
编辑本段C++的typeid
在c++中,typeid用于获知一个变量的具体类型。(注意:typeid是操作符,不是函数!)
运行时获知变量类型名称,可以使用 typeid(变量).name,需要注意不是所有编译器都输出"int"、"float"等之类的名称,对于这类的编译器可以这样使用:float f = 1.1f; if( typeid(f) == typeid(0.0f) ) ……( 即不是所有编译器都能用typeid(int).name输出"int",如果可以用if( typeid(浮点变量)==typeid(浮点常数) ) )
事例代码:
#include <iostream>
using namespace std;
int main( void )
{
// sample 1
cout << typeid(1.1f).name() << endl;
// sample 2
class Base1
{
};
class Derive1 : public Base1
{
};
Derive1 d1;
Base1& b1 = d1;
cout << typeid(b1).name() << endl; // 输出"class Base1",因为Derive1和Base1之间没有多态性
// sample 3, 编译时需要加参数 /GR
class Base2
{
virtual void fun( void ) {}
};
class Derive2 : public Base2
{
};
Derive2 d2;
Base2& b2 = d2;
cout << typeid(b2).name() << endl; // 输出"class Derive2",因为Derive1和Base1之间有了多态性
// sample 4
class Derive22 : public Base2
{
};
// 指针强制转化失败后可以比较指针是否为零,而引用却没办法,所以引用制转化失败后抛出异常
Derive2* pb1 = dynamic_cast<Derive2*>(&b2);
cout << boolalpha << (0!=pb1) << endl; // 输出"true",因为b2本身确实是指向Derive2
Derive22* pb2 = dynamic_cast<Derive22*>(&b2);
cout << boolalpha << (0!=pb2) << endl; // 输出"false",因为b2本身不是指向Derive2
try {
Derive2& rb1 = dynamic_cast<Derive2&>(b2);
cout << "true" << endl;
} catch( bad_cast )
{
cout << "false" << endl;
}
try {
Derive22& rb2 = dynamic_cast<Derive22&>(b2);
cout << "true" << endl;
} catch( ... ) // 应该是 bad_cast,但不知道为什么在VC++6.0中却不行?因为VC++6.0默认状态是禁用 RTTI 的,启用方式:project->setting->c/c++->category->c++ Language 下面第二个复选框选中。
{
cout << "false" << endl;
}
return 0;
}
编辑本段dede typeid
织梦标签内容
是指 typeid 的同级栏目
使用方式
{dede:arclist row=8 type='image.' titlelen='12' orderby=pubdate typeid='3' flag='h'}
{/dede:arclist}
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid
- typeid用法
- typeid 一节
- typeid-4
- typeid -- study
- typeid详解
- epoll序列
- VIM 简易教程(3)
- 有史以来最简单的三层实例(C#)
- Jenkins服务配置
- Android学习笔记---Android平台1.使用dom解析xml文件
- typeid
- 如何隐藏ckeditor的状态栏
- 我为什么不允许团队使用HAML
- UbuntuSkills(Ubuntu 命令技巧)
- tomcat配置文件分析之context.xml
- Rad Studio: 将发布针对移动开发的原生工具了?
- 乌云背后的幸福线
- 十一张图让你轻松学会用VS打包
- poj 2528 Mayor's posters