C++进阶:xxx_cast 类型转换

在C语言中,基本数据类型之间的直接转换不会报错,指针类型之间的转换需要加强转

#include <stdio.h>

int main(){
   
        int n = 10;
        long l = n;
        float f = n;
        char c = 'a';
        n = c ;
        c = n;

        int* p = &n;
        void* v = p;
        p = (int*)v;    // C++中的指针类型强转
}

而c++对数据类型转换做了细分,有四种类型转换,分别是static_cast、 const_cast、 dynamic_cast、 reinterpret_cast

1. static_cast

字符串指针内容字符串指针地址

对字符串指针:

  1. 在c中使用括号直接修改
  2. 在c++中直接使用static_case<const void*>(s)
#include<iostream>
using namespace std;

// ostream& operator << (ostream& os,const char* s)
// ostream& operator << (ostream& os,const void* s)
int main(){
   
        const char* s = "Hello World";
        cout << s << endl;
        cout << (void*)s << endl;        // c的方式
        cout << static_cast<const void*>(s) << endl;      // c++方式的类型强转
}

结果为:

Hello World
0x4009e9
0x4009e9

int整型enum枚举型
直接使用static_case<Week>(0)更改

#include <iostream>
using namespace std;

enum Week{
   
        SUN,MON,TUE,WED,THU,FRI,SAT
};

int main(){
   
        Week day = (Week)0;           // c的方式
        Week day2 = static_cast<Week>(0);        // c++方式的类型强转
}

2. const_cast

有时,我们需要更改const整型的值,但不能直接更改,就需要const_cast更改

下面,有两种方式可以修改const整型变量:

  1. const_cast 引用方式改,即const_cast<int&>(n) = 11;
  2. const_cast 指针方式改,即*const_cast<int*>(p) = 12;
#include<iostream>
using namespace std;

void Test(const int n){
   
        const_cast<int&>(n) = 11;       // 用引用的方式改
        cout << n << endl;

        const int* p = &n;
        *const_cast<int*>(p) = 12;      // 用指针的方式改
        cout << n << endl;
}

int main(){
   
        const int n = 10;     // #define n 10
        // n = 11;        不能改
        const_cast<int&>(n) = 11;       // 用引用的方式改
        cout << n << endl;           // 在这里不会更改

        const int* p = &n;
        // *p = 12;       不能改
        *const_cast<int*>(p) = 12;      // 用指针的方式改
        cout << n << endl;           // 在这里也不会更改

        Test(n);                     // 在这里才会更改
}

结果为:

10
10
11
12

特别注意:
在主函数中定义const整型变量const int n = 10;相当于做了宏定义#define n 10,所以在主函数中是改变不了的

有时,我们需要更改类中const的成员函数,这代表着不能更改类中的成员变量,就需要 const_cast 更改

下面,有三种方式可以修改类中const成员变量:

  1. const_cast 引用方式改,即const_cast<int&>(n) = 3;
  2. const_cast 指针方式改,即const_cast<Simple*>(this)->n = 4;
  3. 关键字 mutable,加了关键字就可以直接用n = 3;更改了
#include<iostream>
using namespace std;

class Simple{
   
        /* mutable */ int n;
public:
        Simple(int n):n(n){
   }
        void Test() const{
             // void Test(const Simple* this) 不会修改成员变量
                // n = 3;         不能改
                const_cast<int&>(n) = 3;             // 用引用的方法改成员变量
                cout << n << endl;

                const_cast<Simple*>(this)->n = 4;      // 用指针的方法改this内的成员变量
                cout << n << endl;
        }
};

int main()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值