1. 程式人生 > >【C++ Primer | 19】運行類型識別

【C++ Primer | 19】運行類型識別

c++ prime nts 結果 new base pri image esp int

type_info類

測試代碼:

 1 #include <iostream>  
 2 #include <typeinfo.h>  
 3 
 4 class Base {
 5 public:
 6     virtual void vvfunc() {}
 7 };
 8 
 9 class Derived : public Base {};
10 
11 using namespace std;
12 int main() {
13     Derived* pd = new Derived;
14     Base* pb = pd;
15 cout << typeid(pb).name() << endl; //prints "class Base *" 16 cout << typeid(*pb).name() << endl; //prints "class Derived" 17 cout << typeid(pd).name() << endl; //prints "class Derived *" 18 cout << typeid(*pd).name() << endl; //
prints "class Derived" 19 delete pd; 20 }

輸出結果:
技術分享圖片

【C++ Primer | 19】運行類型識別