C++常見面試題

來源:互聯網
上載者:User

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

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。