zoukankan      html  css  js  c++  java
  • C++该typeid和dynamic_cast

    1、typeid在没有虚拟函数的(不相关的动态绑定),typeid它只返回操作对象的实际类型

    2、typeid涉及到动态联编问题时(使用基类指针p或者引用p操作派生类对象),typeid(p)返回基类类型,typeid(*p)返回派生类类型;typeud(&p)返回基类类型。typeid(p)返回派生类类型

    3、dynamic_cast在动态联编(实时类型信息)问题中,能够实现基类指针(或引用)和派生类指针(或引用)之间的尝试性动态转换


    #include "stdafx.h"
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    
    class Base
    {
    public:
    	int m_base;
    	virtual void show()
    	{
    		cout<<"This is Base class"<<endl;
    	}
    };
    class Derived:public Base
    {
    public:
    	int m_derived;
    	void show()
    	{
    		cout<<"This is Derived class"<<endl;
    	}
    };
    
    int main(int argc,char* argv[])
    {
    	Base *pb=new Derived();
    
    	cout<<typeid(pb).name()<<endl;
    	cout<<typeid(*pb).name()<<endl;
    
    	system("pause");
    	return 0;
    }


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    (转)消息队列 Kafka 的基本知识及 .NET Core 客户端
    Neo4j学习笔记
    科技论文推荐系统
    下载pubmed数据
    杂项
    Scrapy 知乎验证码
    Scrapy 爬取网站文章
    爬虫基础知识
    Django linux uWsgi Nginx 部署
    DocumentSimilarity
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4625348.html
Copyright © 2011-2022 走看看