Zheng Chu's Blog

让希望永驻


  • 主页

  • 所有专栏

  • 历史文章

  • 标签

  • 关于我

C++---CHAPTER-4---EXPRESSION

Posted on 2019-09-04 Edited on 2020-12-06 In C++ Views:

类型转换

  • 转换成常量:
    1
    2
    3
    4
    int i;
    const int &j = i; //非常量转换成const int 的引用
    const int *p = &i; //非常量的地址转换成const的地址
    int &r = j, *q =p; //错误,不能用const转换为非常量
  • 强制类型转换的形式:

    1
    cast-name<type>(expression);
  • static_cast:只要不包含低层const就可以使用static_cast:

    1
    2
    3
    4
    double slope = static_cast<double>(j) / i; //转换后做浮点数除法

    void *p = &d; // 任何非常量对象的地址都能存入* void
    double *dp = static_cast<double*>(p); //将void*转换回初始的指针类型
  • const_cast:用于函数重载;

# PRIMER
C++---CHAPTER-6---FUNCTION
剑指offer--c++
  • Table of Contents
  • Overview
Zheng Chu

Zheng Chu

90 posts
20 categories
25 tags
GitHub 简书 CSDN E-Mail
  1. 1. 类型转换
© 2021 Zheng Chu
Powered by Hexo v4.2.1
|
Theme – NexT.Pisces v7.3.0
|