关于RTTI和typeid
#include <iostream>
using namespace std;
class A {};
class B: public A {};
class C: public A {};
int main() {
B* b = new B;
C* c = new C;
A* pb = b;
A* pc = c;
cout << "type of *b is " << typeid(*b).name() << endl
<< "type of *c is " << typeid(*c).name() << endl
<< "type of *pb is " << typeid(*pb).name() << endl
<< "type of *pc is " << typeid(*pc).name() << endl;
return 0;
}
输出为:
type of *b is class B
type of *c is class C
type of *pb is class A
type of *pc is class A
按照通常的理解*pb和*pc的运行时类型都不是class A
但是由于没有额外的保存类型信息,所以无法得到其运行时类型
请问解决的办法是不是只有增加虚函数呢?