c++ typeid运算符

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

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

测试:

cpp 复制代码
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运算符能用于类型比较。

基本类型比较,代码:

cpp 复制代码
// 测试基本类型:
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;

打印:

类的比较,代码:

cpp 复制代码
// 类的比较:
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;
相关推荐
祁同伟.1 小时前
【C++】多态
开发语言·c++
rechol1 小时前
C++ 继承笔记
java·c++·笔记
朱嘉鼎2 小时前
C语言之可变参函数
c语言·开发语言
SunkingYang2 小时前
详细介绍C++中捕获异常类型的方式有哪些,分别用于哪些情形,哪些异常捕获可用于通过OLE操作excel异常
c++·excel·mfc·异常捕获·comerror
北冥湖畔的燕雀5 小时前
C++泛型编程(函数模板以及类模板)
开发语言·c++
QX_hao6 小时前
【Go】--map和struct数据类型
开发语言·后端·golang
你好,我叫C小白6 小时前
C语言 循环结构(1)
c语言·开发语言·算法·while·do...while
Evand J8 小时前
【MATLAB例程】基于USBL和DVL的线性回归误差补偿,对USBL和DVL导航数据进行相互补偿,提高定位精度,附代码下载链接
开发语言·matlab·线性回归·水下定位·usbl·dvl
Larry_Yanan9 小时前
QML学习笔记(四十二)QML的MessageDialog
c++·笔记·qt·学习·ui
爱喝白开水a9 小时前
LangChain 基础系列之 Prompt 工程详解:从设计原理到实战模板_langchain prompt
开发语言·数据库·人工智能·python·langchain·prompt·知识图谱