zoukankan      html  css  js  c++  java
  • 类型转换conversion

    算术转换(arithmetic conversion)

      整型提升(integral promotion)

    指针转换

    转换为bool类型

    转换与枚举类型

    转换为const对象

    显式转换

    satic_cast, dynamic_cast, const_cast 和 reinterpreter_cast

     

    static_cast:

     编译器隐式执行的任何类型转换都可以由static_cast显式完成.

    当需要将一个较大的算术类型赋值给较小的类型时,使用强制类型转换非常有用.

    dynamic_cast:

    e.g.: 假定Base是至少带一个虚函数的类,并且Derived类派生于Base类. 如果有一个名为 basePtr的指向Base的指针,就可以像这样在运行时将它强制转换为指向Derived的指针:

    if (Derived *derivedPtr = dynamic_cast<Derived * > (basePtr)

    {

    // use Derived object to which derivedPtr points

    }

    else{

    // use the Base object to which basePtr points

    }

    在运行时,如果basePtr实际指向Derived对象,则转换成功,并且derivedPtr将被初始化为指向basePtr所指的Drived对象;否则,转换的结果是0,意味着将derivedPtr置为0,并且if中的条件失败.

    const_cast:

    用以添加或删除const特性,用const_cast符来执行其他任何类型转换,都会引起编译错误.

    e.g.:

    const char *pc_str;

    char *pc = string_copy( const_cast<char*> (pc_str));

    reinterpreter_cast:

    通常为操作数的位模式提供较低层次的重新解释.

     

    旧式强制类型转换:

    type (expr); //Function-style cast notation

    (type) expr; // C-language-style cast notation.

     

  • 相关阅读:
    Vue生命周期
    Vue-Router
    Vue组件
    Vue基础以及指令
    ES6 常用语法
    缓存、序列化、信号
    四、全局事务的commit和rollback
    三、全局事务begin请求GlobalBeginRequest
    二、分布式事务协调者DefaultCoordinator
    一、seata-server的main启动方法
  • 原文地址:https://www.cnblogs.com/wucg/p/1860046.html
Copyright © 2011-2022 走看看