BZ易风

导航

 

常量转换

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

 对引用转换

#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;
}

 

posted on 2021-08-25 09:28  BZ易风  阅读(76)  评论(0编辑  收藏  举报