您的位置:首页 > 编程语言 > C语言/C++

C++判断指针的类型

2015-06-29 10:40 357 查看
#include<typeinfo>
#include<iostream>
using namespace std;

class A
{
public:
A()
{
a = 0;
}
int a;
};

class  B
{
public:

B()
{
a = 0;
b = 0;
}
int a, b;
};

int main()
{
A clsA;
B clsB;

A *pclsA = new A();
B *pclsB = new B();

cout << typeid(clsA).name() << endl;
cout << typeid(clsB).name() << endl;

cout << typeid(pclsA).name() << endl;
cout << typeid(pclsB).name() << endl;

if(typeid(clsA) == typeid(*pclsA))
{
cout << "类型相同1" << endl;
}
if(typeid(clsA) == typeid(*pclsB))
{
cout << "类型相同2" << endl;
}
if(typeid(clsB) == typeid(*pclsB))
{
cout << "类型相同3" << endl;
}

system("pause");
return 0;
}

输出结果:

1A

1B

P1A

P1B

类型相同1

类型相同3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: