当前位置: 首页 > news >正文

c++ typeid运算符

typeid运算符能获取类型信息。获取到的是type_info对象。type_info类型如下:

可以看到,这个类删除了拷贝构造函数以及等号操作符。有一些成员函数:hash_code、before、name、raw_name,  还重载了==和!=运算符。

测试:

void testTypeId() {//获取一个普通变量的类型信息unsigned int n = 9527;const type_info& nInfo = typeid(n); // 得到type_info 对象std::cout << "普通变量的类型信息:" << nInfo.name() << " | " << nInfo.raw_name() << " | " << nInfo.hash_code() << endl;//获取一个普通类型的类型信息const type_info& charInfo = typeid(char);std::cout << "普通类型的类型信息:" << charInfo.name() << " | " << charInfo.raw_name() << " | " << charInfo.hash_code() << endl;//获取一个字面量的类型信息const type_info& dInfo = typeid(95.27);std::cout << "字面量的类型信息:" << dInfo.name() << " | " << dInfo.raw_name() << " | " << dInfo.hash_code() << endl;class Base {};struct MyStruct {};//获取一个对象的类型信息Base obj;const type_info& objInfo = typeid(obj);std::cout << "对象的类型信息:" << objInfo.name() << " | " << objInfo.raw_name() << " | " << objInfo.hash_code() << endl;//获取一个结构体的类型信息const type_info& stuInfo = typeid(struct MyStruct);std::cout << "结构体的类型信息:" << stuInfo.name() << " | " << stuInfo.raw_name() << " | " << stuInfo.hash_code() << endl;//获取一个表达式的类型信息const type_info& expInfo = typeid(9528 - 1);std::cout << "表达式的类型信息:" << expInfo.name() << " | " << expInfo.raw_name() << " | " << expInfo.hash_code() << endl;
}

打印:

typeid运算符能用于类型比较。

基本类型比较,代码:

// 测试基本类型:
char* str;
int a = 9527;
int b = 1;
float f;
std::cout << "typeid(int) == typeid(int) ? " << (typeid(int) == typeid(int)) << endl;
std::cout << "typeid(int) == typeid(char) ? " << (typeid(int) == typeid(char)) << endl;
std::cout << "typeid(char) == typeid(char*) ? " << (typeid(char) == typeid(char*)) << endl;
std::cout << "typeid(str) == typeid(char*) ? " << (typeid(str) == typeid(char*)) << endl;
std::cout << "typeid(a) == typeid(int) ? " << (typeid(a) == typeid(int)) << endl;
std::cout << "typeid(a) == typeid(b) ? " << (typeid(a) == typeid(b)) << endl;
std::cout << "typeid(a) == typeid(f) ? " << (typeid(a) == typeid(f)) << endl;
std::cout << "typeid(a - b) == typeid(int) ? " << (typeid(a - b) == typeid(int)) << endl;

打印:

类的比较,代码:

// 类的比较:
class Sub : public Base {};
Base obj1;
Base* p1;
Sub obj2;
Sub* p2 = new Sub;
p1 = p2;
std::cout << "typeid(obj1) == typeid(p1) ? " << (typeid(obj1) == typeid(p1)) << endl;
std::cout << "typeid(obj1) == typeid(*p1) ? " << (typeid(obj1) == typeid(*p1)) << endl;
std::cout << "typeid(obj1) == typeid(obj2) ? " << (typeid(obj1) == typeid(obj2)) << endl;
std::cout << "typeid(p1) == typeid(Base*) ? " << (typeid(p1) == typeid(Base*)) << endl;

http://www.xdnf.cn/news/746335.html

相关文章:

  • 如何打包conda环境从一台电脑到另外一台电脑
  • 电力高空作业安全检测(3)RT-DETR模型
  • MySQL高级查询技巧:分组、聚合、子查询与分页【MySQL系列】
  • 深入理解CSS常规流布局
  • 【系统架构设计师】第一章 计算机硬件 1.1 计算机硬件 - CPU - 校验码
  • Unity 模拟高度尺系统开发详解——实现拖动、范围限制、碰撞吸附与本地坐标轴选择
  • Linux基本指令/下
  • 信息安全之为什么引入公钥密码
  • Linux系统下安装配置 Nginx
  • AUTOSAR图解==>AUTOSAR_EXP_AIADASAndVMC
  • 数组题解——最大子数组和​【LeetCode】
  • 机器学习算法04:SVC 算法(向量机分类)
  • Fastapi 学习使用
  • [GHCTF 2025]SQL???
  • 23种设计模式概览
  • ubuntu20.04.5--arm64版上使用node集成java
  • Ubuntu22.04通过命令行安装qt5
  • FPGA纯verilog实现MIPI-DSI视频编码输出,提供工程源码和技术支持
  • Redis7底层数据结构解析
  • [VMM]虚拟地址到物理地址的三级或四级页表查找过程详解
  • 4000万日订单背后,饿了么再掀即时零售的“效率革命”
  • win1011安装WinGet和Windows Terminal
  • CQF预备知识:一、微积分 -- 1.8 多变量函数:多元微积分详解
  • 离线安装 Python 包及其全部依赖
  • 谷歌Stitch:AI赋能UI设计,免费高效新利器
  • Vue2+Vuex通过数组动态生成store数据(分组模式)
  • 类FNAF游戏后续
  • Constraints and Triggers
  • 零基础一站式端游内存辅助编写教程(无密)
  • 进程间通信(信号量)