• dynamic_cast 与 typeid


     C++中的类型转换分为两种:

    1. 隐式类型转换;
    2. 显式类型转换。

      隐式类型转换一般都是不经意间就发生了,比如int + float 时,int就被隐式的转换为float类型了。

      显示类型转换包括四种方式:dynamic_cast、static_cast、const_cast、reinterpret_cast

     static_cast

      static_cast的转换格式:static_cast <type-id> (expression)

      将expression转换为type-id类型,主要用于非多态类型之间的转换,不提供运行时的检查来确保转换的安全性。主要在以下几种场合中使用:

    1. 用于类层次结构中,基类和子类之间指针和引用的转换;
      当进行上行转换,也就是把子类的指针或引用转换成父类表示,这种转换是安全的;
      当进行下行转换,也就是把父类的指针或引用转换成子类表示,这种转换是不安全的,也需要程序员来保证;
    2. 用于基本数据类型之间的转换,如把int转换成char,把int转换成enum等等,这种转换的安全性需要程序员来保证;
    3. 把void指针转换成目标类型的指针,是及其不安全的;

      注:static_cast不能转换掉expression的const、volatile和__unaligned属性。

     dynamic_cast

        dynamic_cast的转换格式:dynamic_cast <type-id> (expression)

      dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。

      在多态类型之间的转换主要使用dynamic_cast,因为类型提供了运行时信息。

     const_cast

      const_cast的转换格式:const_cast <type-id> (expression)

      const_cast用来将类型的const、volatile和__unaligned属性移除。常量指针被转换成非常量指针,并且仍然指向原来的对象;常量引用被转换成非常量引用,并且仍然引用原来的对象。

     reinterpret_cast

      reinterpret_cast的转换格式:reinterpret_cast <type-id> (expression)

      允许将任何指针类型转换为其它的指针类型;它主要用于将一种数据类型从一种类型转换为另一种类型。它可以将一个指针转换成一个整数,也可以将一个整数转换成一个指针,在实际开发中,先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原来的指针值;特别是开辟了系统全局的内存空间,需要在多个应用程序之间使用时,需要彼此共享,传递这个内存空间的指针时,就可以将指针转换成整数值,得到以后,再将整数值转换成指针,进行对应的操作。

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <typeinfo>
    
    using namespace std;
    
    class base{
    public:
        virtual void funcA(){
            cout<<"base"<<endl;
        }
    };
    
    class derived: public base{
    public:
        virtual void funcB(){
            cout<<"derived"<<endl;
        }
    };
    
    void funcC(base *p)
    {
        //dynamic_cast会检查p里面是否真存有派生类derived类型的指针,如果存在那么返回该类型的指针,否则返回NULL
        derived* dp = dynamic_cast<derived*>(p);
        if(dp){
            dp -> funcB();
        }else{
            p -> funcA();
        }
    }
    
    void funcD(base *p)
    {
        derived *dp = NULL;
        if(typeid(*p) == typeid(derived)){
            dp = static_cast<derived*>(p);
            dp -> funcB();
        }else{
            p -> funcA();
        }
    }
    
    int main()
    {
        base *cp = new derived();
        cout<<typeid(cp).name()<<endl;//base
        cout<<typeid(*cp).name()<<endl;//derived
        funcD(cp);//derived
        funcC(cp);//derived
        base *dp = new base();
        funcC(dp);//base
        funcD(dp);//base
        return 0;
    }
  • 相关阅读:
    尝试用微博记录 SQL Server 2012开发者训练营笔记
    Contact Manager Web API 示例[4] 异常处理(Exception Handling)
    使用IAPIExplorer列举ASP.NET Web API
    从 WebAPI Beta 更新到WebAPI RC
    Tracing in ASP.NET Web API
    一个基于asp.net2.0空间的webproxy程序:ASProxy
    微软发布平板电脑 – Surface
    Big Data, Hadoop and StreamInsight™
    REST 入门介绍
    通过企业分布式缓存共享运行时数据
  • 原文地址:https://www.cnblogs.com/luntai/p/5875050.html
Copyright © 2020-2023  润新知