题目: 关于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, 静态转型:
主要是数据类型的转换, 还可以用于继承;
代码如下:
/* * 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(); }
输出:
常量性转除: 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
更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索c++
, 代码
, 常量
, 代码 试题
, 解释
, const_cast
, cast
, 向下转型
, 指向
, reinterpret_cast
, 常见面试题
, static_cast
, js常见面试题
java常见面试题
c++ 面试题、c语言常见面试题、java常见面试题、spring常见面试题、php常见面试题,以便于您获取更多的相关知识。