Base *bp;
Derived *dp;
// compare type at run time of two objects
if (typeid(*bp) == typeid(*dp)) {
// bp and dp point to objects of the same type
}
// test whether run time type is a specific type
if (typeid(*bp) == typeid(Derived)) {
// bp actually points to a Derived
}
注意,typeid 的操作数是表示对象的表达式——测试 *bp,而不是 bp:
Base *bp = new Derived ;
cout<<typeid(bp)<<endl; //输出 Class Base *
cout<<typeid(*bp)<<endl; //输出 Class Derived