C++新型强制类型转换。

  C++强制类型转换分为4个不同的类型。

  1、static_cast

    -用作基本类型转换

    -不能用于基本类型指针转换。

    -可以用于有继承关系对象之间的转换和类指针之间的转换。

#include <stdio.h>

void static_cast_demo(void)
{
    int i = 0x12345;
    char c = 'c';
    int* pi = &i;  //pi -> i
    char* pc = &c; //pc -> c
    
    c = static_cast<char>(i);//int i change to char c
    pc = static_cast<char*>(pi);//想通过 static_cast 将int 型指针转换为 char型指针。由于static_cast不能用于基本类型指针转换error
}



int main(int argc, char *argv[])
{
    
    
    return 0;
}

编译结果:  

test.cpp: In function ‘void static_cast_demo()’:
test.cpp:12:28: error: invalid static_cast from type ‘int*’ to type ‘char*’

  2、const_cast

    -用于去除变量的只读属性。

    -强制内心转换的目标只能是指针或者引用  

/*强制类型转换*/
#include <stdio.h>

void const_cast_demo(void)
{
    const int& j = 1;//定义一个j引用常量,j拥有只读属性
    int& k = const_cast<int&>(j);//定义一个k,通过const_cast k去除了j所拥有的只读属性
    
    const int x = 2; //定义一个常量x   x拥有只读属性
    int& y = const_cast<int&>(x);//定义一个y 通过const_cast y去除了引用x时所拥有的只读属性,所以y是可变的
    
    int z = const_cast<int>(x);//想通过const_cast 来将x转换为一个int型变量,这不允许,因为const_cast 强制转换的目标只能是指针或者引用。error
    
    k = 5;
    
    printf("k = %d\n", k);
    printf("j = %d\n", j);
    
    y = 8;
    
    printf("x = %d\n", x);
    printf("y = %d\n", y);
    printf("&x = %p\n", &x);
    printf("&y = %p\n", &y);

}


int main(int argc, char *argv[])
{
    
    const_cast_demo();
    return 0;
}

编译结果:  

test.cpp: In function ‘void const_cast_demo()’:
test.cpp:12:30: error: invalid use of const_cast with type ‘int’, which is not a pointer, reference, nor a pointer-to-data-member type

  3、dynamic_cast

    -用于指针类型之间的强制类型转换。

    -用于整数和指针类型之间的强制类型转换。

#include <stdio.h>

void dynamic_cast_demo()
{
    int i = 0;
    int* pi = &i;
    char* pc = dynamic_cast<char*>(pi);//dynamic_cast 只能用于整数与指针类型之间的相互转换,这里将 整数类型转换为 char*类型 error
}
int main(int argc, char *argv[])
{
    
    
    return 0;
}

编译结果:  

test.cpp: In function ‘void dynamic_cast_demo()’:
test.cpp:8:38: error: cannot dynamic_cast ‘pi’ (of type ‘int*’) to type ‘char*’ (target is not pointer or reference to class)

  4、reinterpret_cast

    -用于有继承关系类指针之间的转换。

    -用于有交叉关系类指针之间的转换。

    -具有类型检查功能

    -需要虚函数的支持。    

#include <stdio.h>

void reinterpret_cast_demo()
{
    int i = 0;
    char c = 'c';
    int* pi = &i;
    char* pc = &c;
    
    pc = reinterpret_cast<char*>(pi);//将int* 类型指针 转换为 char*
    pi = reinterpret_cast<int*>(pc);//将char* 类型指针 转换为 int*
    pi = reinterpret_cast<int*>(i);//将int 类型转换为 int*类型指针
    c = reinterpret_cast<char>(i); //想 通过reinterpret_cast  将int型 转换为char型 普通类型转换使用static_cast error 
}
int main(int argc, char *argv[])
{
    
    
    return 0;
}

   编译结果:   

test.cpp: In function ‘void reinterpret_cast_demo()’:
test.cpp:14:33: error: invalid cast from type ‘int’ to type ‘char’

用法格式:xxx_cast <type>(Expression)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DipsyHu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值