C++ Primer 5th笔记(chap 19 特殊工具与技术)type_info 类

Posted thefist11

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ Primer 5th笔记(chap 19 特殊工具与技术)type_info 类相关的知识,希望对你有一定的参考价值。

1. type_info 的操作

操作描述
t1 == t2如果type_info对象t1和t2表示同一种类型,则返回true
t1 != t2如果type_info对象t1和t2表示不同的类型,则返回true
t.name()返回一个C风格字符串,表示类型名字的可打印形式
t1.before(t2)返回一个bool值,表示t1是否位于t2之前,顺序关系依赖于编译器
  • type_info类没有默认构造函数
  • 它的拷贝和移动构造函数以及赋值运算符都被定义为删除的。无法定义或者拷贝type_info类型的对象,也不能为type_info对象赋值。
  • 创建type_info对象的唯一途径就是使用typeid运算符。

1.1 name函数

int arr[10];
Derived d;
Base* p = &d;

cout << typeid(42).name() << endl;
cout << typeid(arr).name() << endl;
cout << typeid(std::string).name() << endl;
cout << typeid(d).name() << endl;
cout << typeid(p).name() << endl;

输出结果:

int
int [10]
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
class Derived
class Base *

以上是关于C++ Primer 5th笔记(chap 19 特殊工具与技术)type_info 类的主要内容,如果未能解决你的问题,请参考以下文章