首页 > 其他分享 >四种类型转换符

四种类型转换符

时间:2023-06-19 11:55:05浏览次数:16  
标签:类型转换 std cout void value cast static 四种

C++中常见的面试题.

题目: 关于C++中, 四种类型转换的关键字, 的详解, 也可以给出代码, 判断输出 或 判断哪些代码有误.

 

答案及范例如下:

 

四种关键字: const_cast, 常量性转除;dynamic_cast, 向下安全转型;reinterpret_cast, 重新解释转型static_cast, 静态转型;

1. const_cast, 常量性转除:

主要对变量的常量性(const)进行操作, 移除变量的常量性, 即可以被非常量指向和引用, 详见代码;

2. dynamic_cast, 向下安全转型:

主要应用于继承体系, 可以由 指向派生类的基类部分的指针, 转换指向派生类指向兄弟类;

static_cast只能转换为指向派生类;

3. reinterpret_cast, 重新解释转型:

主要是对2进制数据进行重新解释(re-interpret),不改变格式, 而static_cast会改变格式进行解释;

由派生类转换基类, 则重新解释转换, 不改变地址, 静态转换改变地址;

4. static_cast, 静态转型:

主要是数据类型的转换, 还可以用于继承;

 

代码如下:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

/*

 * cppprimer.cpp

 *

 *  Created on: 2014.2.10

 *      Author: Spike

 */

 

/*eclipse cdt, gcc 4.8.1*/

 

#include <iostream>

 

/*常量性移除指针详解*/

 

struct S {

    S() : value(0) {}

    int value;

};

 

void CastConst (void)

{

    const S s;

    std::cout << s.value =  << s.value << std::endl;

    //S* ps = &s; //error, 指向常量

    S* ps = const_cast<s*>(&s);

    ps->value = 1;

    std::cout << s.value =  << s.value << std::endl;

    //S& rs = s; //error, 引用常量

    S& rs = const_cast<s&>(s);

    rs.value = 2;

    std::cout << s.value =  << s.value << std::endl;

}

 

/*安全向下转型*/

 

struct B /*基类B*/ {

    virtual void f() { std::cout << Base::f << std::endl; }

    void thisf() { std::cout << Base::thisf << std::endl;}

    virtual ~B() {}

};

 

struct B2 /*基类B2*/ {

    virtual void g() { std::cout << Base2::g << std::endl; }

    void thisg() { std::cout << Base2::thisg << std::endl;}

    virtual ~B2() {}

};

 

struct D : public B, public B2 /*派生类D*/ {

    virtual void f() { std::cout << Derived::f << std::endl; }

    virtual void g() { std::cout << Derived::g << std::endl; }

    virtual ~D() {}

};

 

void CastDynamic (void)

{

     B* pB_D = new D;

     pB_D->f();

     //pD->g(); //error, 只包含B部分

 

     D *pD_D = dynamic_cast<d*>(pB_D); //转换为派生类

     pD_D->g();

     B2* pB2_D = dynamic_cast<b2*>(pB_D); //转换为兄弟类

     pB2_D->g();

 

     D *pD_Ds = static_cast<d*>(pB_D);

     pD_Ds->g();

     //B2* pB2_Ds = static_cast<b2*>(pB_D); //error, 不能转换为兄弟类

}

 

/*重新解释转型*/

 

struct rA { int m_a; };

struct rB { int m_b; };

struct rC : public rA, public rB {};

 

void CastReinterpret (void)

{

    int *i= new int;

    *i = 10;

    std::cout << *i =  << *i << std::endl;

    std::cout << i =  << i << std::endl;

    double *d=reinterpret_cast<double*> (i);

    std::cout << *d =  << *d << std::endl;

    std::cout << d =  << d << std::endl;

 

    rC c;

    std::cout << &c =  << &c << std::endl

            << reinterpret_cast<rb*>(&c) =  <<reinterpret_cast<rb*>(&c) << std::endl

            << static_cast <rb*>(&c) =  << static_cast <rb*>(&c) << std::endl

            << reinterpret_cast<ra*>(&c) =  <<reinterpret_cast<ra*>(&c) << std::endl

            << static_cast <ra*>(&c) =  << static_cast <ra*>(&c) << std::endl

            << std::endl;

}

 

int main (void)

{

    std::cout << std::endl << 常量性转除: << std::endl;

    CastConst();

    std::cout << std::endl << 安全向下转型: << std::endl;

    CastDynamic();

    std::cout << std::endl << 重新解释转型: << std::endl;

    CastReinterpret();

}

</ra*></ra*></reinterpret_cast<ra*></ra*></rb*></rb*></reinterpret_cast<rb*></rb*></double*></b2*></d*></b2*></d*></s&></s*></iostream>

 

输出:

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

常量性转除:

s.value = 0

s.value = 1

s.value = 2

 

安全向下转型:

Derived::f

Derived::g

Derived::g

Derived::g

 

重新解释转型:

*i = 10

i = 0x471718

*d = 2.55917e-307

d = 0x471718

&c = 0x22feb0

reinterpret_cast<rb*>(&c) = 0x22feb0

static_cast <rb*>(&c) = 0x22feb4

reinterpret_cast<ra*>(&c) = 0x22feb0

static_cast <ra*>(&c) = 0x22feb0

</ra*></ra*></rb*></rb*>

 

 

https://www.cnblogs.com/daocaorenblog/p/5279077.html

标签:类型转换,std,cout,void,value,cast,static,四种
From: https://www.cnblogs.com/im18620660608/p/17490811.html

相关文章

  • JS(简单数据类型、数据类型转换)
    一.数据类型简介1.1为什么需要数据类型在计算机中,不同的数据所需占用的存储空间是不同的,为了便于把数据分成所需内存大小不同的数据,充分利用存储空间,于是定义了不同的数据类型。简单来说,数据类型就是数据的类别型号。比如姓名“张三”,年龄18,这些数据的类型是不一样的。1.2变......
  • c++强制类型转换的不同
    参考https://blog.csdn.net/luolaihua2018/article/details/111996610https://zhuanlan.zhihu.com/p/368267441https://zhuanlan.zhihu.com/p/151744661https://blog.csdn.net/Behold1942/article/details/111657231https://stackoverflow.com/questions/332030/when-sho......
  • Qt编写视频监控系统79-四种界面导航栏的设计
    一、前言最初视频监控系统按照二级菜单的设计思路,顶部标题栏一级菜单,左侧对应二级菜单,最初采用图片在上面,文字在下面的按钮方式展示,随着功能的增加,二级菜单越来越多,如果都是这个图文上下排列的按钮,那左侧高度空间不够,比如在笔记本1366x768的分辨率上,左侧如果有七八个菜单按钮,那就......
  • C++类型转换详解--const_cast
    一.函数描述:const_cast<type-id>(expression)主要是用来去掉const属性,当然也可以加上const属性。主要是用前者,后者很少用。去掉const属性:const_case<int*>(&num),常用,因为不能把一个const变量直接赋给一个非const变量,必须要转换。加上const属性:constint*k=const_case......
  • java 中四种引用类型(对象的强、软、弱和虚引用)
    对象的强、软、弱和虚引用在JDK1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象。也就是说,只有对象处于可触及(reachable)状态,程序才能使用它。从JDK1.2版本开始,把对象的引用分为4种级别,从而使程序能更加灵活地控制对象的生命周期。这4种级别由......
  • hive的四种导入数据方式 两种数据导出方式
     Hive并不支持INSERT INTO …. VALUES形式的语句,也不支持针对某些列插入数据形式,hive操作的是海量文件数据,是没办法区分向那些列插入数据的,正是因为操作了海量数据,因此无法做到事务,循环. Hive的几种常见的数据导入方式这里介绍四种:(1)、从本地文件系统中导入数据到Hive表;(2)、......
  • django中设置字段动态默认时间的四种方式
    1.create_time_one=models.DateTimeField('创建时间',default=datetime.now())2.update_time_one=models.DateTimeField('更新时间',default=datetime.now)3.create_time_tow=models.DateTimeField('创建时间',auto_now_add=True)4.update_ti......
  • mybatis批量插入的四种方式
    一、循环插入publicvoidinsert(List<User>userList){userList.forEach(user->userDao.insert(user));}<insertid="insert">INSERTINTO`demo`.`user`(`username`,`address`,`remark`,`age`,`create_time`)VALUES(#{u......
  • 利用dotnet core的代码生成实现类型转换
    利用dotnetcore的代码生成的特性,自动生成类型转换的代码。类似于AutoMaper,但是代码生成近似于手写代码,不用反射,性能更好生成通过比较属性名字(不区分大小写)属性支持简单类型,类,List,Dictionary(key最好是string类型)在需要转换的类上标记特性:ConvertFrom、ConvertTo[Conv......
  • redis四种模式
    1、单机模式单机模式就是在一台服务器上安装redis,然后启动,所有业务都调用这一台redis服务器。优点:部署简单,只需要在一台服务器上安装并启动redis就行。成本低,没有备用节点,不需要其他的开支。高性能,单机不需要同步数据,数据天然一致性。缺点:可靠性较弱,一旦服务器宕机,所有业务......