作者xxocean (keep walking)
看板C_and_CPP
标题[问题] RTTI typeid 的用法
时间Wed Feb 15 16:38:01 2006
以下是我照着书上范例打的,还有output结果
使用Dev-c++编译~
#include<iostream>
#include<typeinfo>
using namespace std;
class BaseClass{
virtual void f() {};
//...
};
class Derived1 : public BaseClass{
};
class Derived2 : public BaseClass{
};
int main()
{
double j;
BaseClass *p, baseob;
Derived1 ob1;
Derived2 ob2;
cout << "Typeid of j is: ";
cout << typeid(j).name() << endl;
p = &baseob;
cout << "p is pointing to an object of type: ";
cout << typeid(*p).name() << endl;
p = &ob1;
cout << "p is pointing to an object of type: ";
cout << typeid(*p).name() << endl;
p = &ob2;
cout << "p is pointing to an object of type: ";
cout << typeid(*p).name() << endl;
system("pause");
return 0;
}
结果:
Typeid of j is: d
p is pointing to an object of type: 9BaseClass
p is pointing to an object of type: 8Derived1
p is pointing to an object of type: 8Derived2
问题:
请问为什麽output那边三个type name之前会各有一个数字呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.116.245.211
1F:推 godfat:字串长度 02/15 16:48
2F:→ godfat:更正,class name 长度... 02/15 16:48
3F:推 xxocean:谢谢~ 不知道是我这本书太旧还是有误 他这个范例档跑出来 02/15 17:54
4F:→ xxocean:的结果没有长度值... 02/15 17:56
5F:推 godfat:std::type_info::name() 是 compiler 实作,标准没规定 02/15 18:42
6F:→ godfat:所以会跑出什麽字串是无法预期的(如果compiler不可预期) 02/15 18:43
7F:→ godfat:gcc 3.4 提供的名字老实说很难懂,仅供参考罢了 02/15 18:44
8F:推 xxocean:原来如此~ 感谢您^^ 02/16 00:44