C++:typeid()查看变量类型

调用typeid函数查看变量类型

头文件

#include <typeinfo>

typeid()语法为

char type[] = typeid(object).name();

返回值为const char * 字符串类型

以下为一些实例

	//无符号整型
	int int_num = 1;
	cout << "int_num的类型是" << typeid(int_num).name() << endl;
	cout << endl;
	//有符号整型
	unsigned int unsigned_int_num = 1;
	cout << "unsigned_num的类型是" << typeid(unsigned_int_num).name() << endl;
	cout << endl;

	//字符
	char s0 = '0';
	cout << "s0的类型是" << typeid(s0).name() << endl;
	cout << endl;
	//字符数组(字符串)
	char s1[] = "01";
	cout << "s1的类型是" << typeid(s1).name() << endl;
	cout << endl;
	//字符串
	string s2 = "0";
	cout << "s2的类型是" << typeid(s2).name() << endl;
	cout << endl;
	
	//指针
	int * p = &int_num;
	cout << "p的类型是" << typeid(p).name() << endl;
	cout << endl;

	//结构体
	struct temp1
	{
		int data;
	};

	cout << "temp1的类型是" << typeid(temp1).name() << endl;
	cout << endl;

	//类
	class temp2 
	{
	private:
		int data;
	public:
		temp2()
		{

		}
		~temp2()
		{

		}
	};

	cout << "temp2的类型是" << typeid(temp2).name() << endl;
	cout << endl;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值