常量转换

不能对非指针或者非引用进行转换

类型转换(常量转换和引用转换)

 对引用转换

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

void test01()
{
    const int* p = NULL;
    //取出const
    int* newp = const_cast<int*>(p);  //去除const

    int* p2 = NULL;
    const int* newP2 = const_cast<const int*>(p2);

    //不能对非指针 或 非引用的 变量进行转换
    //const int a = 10;
    //int b = const_cast<int>(a);

    //引用
    int num = 10;
    int& numRef = num;

    const int& numRef2 = static_cast<const int&>(numRef);
}

int main()
{
    system("Pause");
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-06-01
  • 2022-12-23
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
猜你喜欢
  • 2022-12-23
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2021-11-15
  • 2022-12-23
相关资源
相似解决方案