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