題目: 關於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