typeid获取数据类型

用typeid获取数据类型

 typeid在c++中是关键字,不是函数.先上代码看看怎么使用.
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
	int x = 10;
	double y = 10.0;

	printf("x type is:%s\n", sizeof(x));

	cout << "x type is:" << typeid(x).name() << endl;
	cout << "y type is:" << typeid(y).name() << endl;

	cin.get();
	return 0;
}

上边的代码,看看在vs编译的效果:

typeid输出数据类型,在vs2017输出

在gcc中编译的时候,发现编译错是没有引入typeinfo

gcc编译错误,没有引入typeinfo

修改源文件,引入typeinfo.重新编译效果:

gcc编译,typeid输出数据类型

vs在编译的时候,自动引入typeifo,在编译的时候隐匿不少细节呀!

秋风 2017-06-21