码迷,mamicode.com
首页 > 编程语言 > 详细

【C++ Primer | 19】运行类型识别

时间:2018-08-25 21:49:28      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:http   ase   int   prime   eid   void   space   stream   out   

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】运行类型识别

标签:http   ase   int   prime   eid   void   space   stream   out   

原文地址:https://www.cnblogs.com/sunbines/p/9535448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!