调用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;