C++ 关键字 typeid
2016-08-24 17:48
357 查看
typeid 关键字的作用:返回指针或者引用所指对象的实际类型。
用法:typeid(int).name(); 类似于sizeof()
//typeid(type) //type可以为类,结构体,公用体的类型,对象,指针,函数,函数指针,int,double等数据类型
#include <iostream>
#include <typeinfo>
using namespace std;
struct data_t
{
int a;
double b;
};
int main()
{
data_t dat;
cout << "typeid(dat):\t" << typeid(dat).name() << endl;
cout << "typeid(int):\t" << typeid(int).name() << endl;
cout << "typeid(double):\t" << typeid(double).name() << endl;
cout << "sizeof(int):\t" << sizeof(int) << endl;
cout << "sizeof(double):\t" << sizeof(double) << endl;
cout << "sizeof(data_t):\t" << sizeof(data_t) << endl;
}输出结果:
typeid(dat): struct data_t
typeid(int): int
typeid(double): double
sizeof(int): 4
sizeof(double): 8
sizeof(data_t): 16
用法:typeid(int).name(); 类似于sizeof()
//typeid(type) //type可以为类,结构体,公用体的类型,对象,指针,函数,函数指针,int,double等数据类型
#include <iostream>
#include <typeinfo>
using namespace std;
struct data_t
{
int a;
double b;
};
int main()
{
data_t dat;
cout << "typeid(dat):\t" << typeid(dat).name() << endl;
cout << "typeid(int):\t" << typeid(int).name() << endl;
cout << "typeid(double):\t" << typeid(double).name() << endl;
cout << "sizeof(int):\t" << sizeof(int) << endl;
cout << "sizeof(double):\t" << sizeof(double) << endl;
cout << "sizeof(data_t):\t" << sizeof(data_t) << endl;
}输出结果:
typeid(dat): struct data_t
typeid(int): int
typeid(double): double
sizeof(int): 4
sizeof(double): 8
sizeof(data_t): 16
相关文章推荐
- C++ 关键字typeid
- C++ 关键字typeid
- C++ 关键字typeid
- C++中的关键字--typeid
- C++ typeid关键字详解
- C++ 关键字 typeid, typename
- C++ typeid关键字详解
- C++中的typeid关键字
- C++ 关键字 typeid
- C++中的typeid关键字
- C++ 关键字typeid
- C++ 关键字typeid
- C++ 关键字typeid
- C++ 关键字typeid
- C++中的typeid关键字
- C++ 关键字 typeid, typename
- C++ 关键字typeid
- C++ 关键字typeid
- C++ 关键字typeid
- C++ 关键字typeid 运行时类型检查