读书人

一个关于typeid的有关问题

发布时间: 2012-03-04 11:13:34 作者: rapoo

一个关于typeid的问题
#include <iostream>
#include <typeinfo>
using namespace std;
class myclass1
{public:myclass1( ){cout < < "this is myclass1.\n ";}
};
class myclass2
{public:myclass2( ){cout < < "this is myclass2.\n ";}
};
int main( )
{int i=0,j=0;
float f=0;
myclass1 ob1;
myclass2 ob2;
cout < < "The type of i is: " < <typeid(i).name( ) < <endl;
cout < < "The type of f is: " < <typeid(f).name( ) < <endl;
cout < < "The type of ob1 is: " < <typeid(ob1).name( ) < <endl;
if(typeid(i)==typeid(j)) cout < < "The type of i and j are the same.\n ";
if(typeid(ob1)!=typeid(ob2)) cout < < "ob1 and ob2 are of differing types.\n ";
cin.get();
return 0;
}

运行结果:
this is myclass1.
this is myclass2.
The type of i is:i
The type of f is:f
The type of ob1 is:8myclass1 //请问,这里怎么会有个8?应该只是显示ob1的类型名吧!
The type of i and j are the same.
ob1 and ob2 are of differing types.

[解决办法]
有也正常没有也正常。C++没规定应该输出什么。
[解决办法]
不同的编译器输出也不一样哦。
vc8输出的名字比较不错。

读书人网 >C++

热点推荐