typeid 的用法

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章