typeid_2.cpp

来自「本代码是《C/C++程序员实用大全》的配套代码。网络转载」· C++ 代码 · 共 36 行

CPP
36
字号
#include <iostream.h>
#include <typeinfo.h>

class Base {
   int a, b;
   virtual void func(void) {};
 };

class Derived1: public Base {
   int i, j;
 }

class Derived2: public Base {
   int k;
 }

void main(void)
 {
   int i;
   Base *p, baseobj;
   Derived1 obj1;
   Derived2 obj2;

   cout << "Typeid of i is: ";
   cout << typeid(i).name() << endl;
   p = &baseobj;
   cout << "p is currently pointing to an object of type: ";
   cout << typeid(*p).name() << endl;
   p = & obj1;
   cout << "p is now pointing to an object of type: ";
   cout << typeid(*p).name() << endl;
   p = & obj2;
   cout << "p is finally pointing to an object of type: ";
   cout << typeid(*p).name() << endl;
 }
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?