久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

派生類和基類之間指針到指針的轉(zhuǎn)換?

Conversion of pointer-to-pointer between derived and base classes?(派生類和基類之間指針到指針的轉(zhuǎn)換?)
本文介紹了派生類和基類之間指針到指針的轉(zhuǎn)換?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

關(guān)于以下 C++ 程序:

Regarding the following C++ program:

class Base { };

class Child : public Base { };

int main()
{   
    // Normal: using child as base is allowed
    Child *c = new Child();
    Base *b = c;

    // Double pointers: apparently can't use Child** as Base**
    Child **cc = &c;
    Base **bb = cc;

    return 0;
}

GCC 在最后的賦值語句上產(chǎn)生以下錯誤:

GCC produces the following error on the last assignment statement:

error: invalid conversion from ‘Child**’ to ‘Base**’

我的問題分為兩部分:

  1. 為什么沒有從 Child** 到 Base** 的隱式轉(zhuǎn)換?
  2. 我可以使用 C 樣式轉(zhuǎn)換或 reinterpret_cast 使這個示例工作.使用這些強(qiáng)制轉(zhuǎn)換意味著拋棄所有類型安全.有什么我可以添加到類定義中以使這些指針隱式轉(zhuǎn)換的,或者至少以一種允許我使用 static_cast 的方式來表達(dá)轉(zhuǎn)換?
  1. Why is there no implicit conversion from Child** to Base**?
  2. I can make this example work with a C-style cast or a reinterpret_cast. Using these casts means throwing away all type safety. Is there anything I can add to the class definitions to make these pointers cast implicitly, or at least phrase the conversion in a way that allows me to use static_cast instead?

推薦答案

如果允許,你可以這樣寫:

If this was allowed, you could write this:

*bb = new Base;

而且c 最終會指向Base 的一個實例.不好.

And c would end up pointing to an instance of Base. Bad.

這篇關(guān)于派生類和基類之間指針到指針的轉(zhuǎn)換?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 黄网站涩免费蜜桃网站 | 亚洲婷婷六月天 | 狠狠av| 国产成人精品综合 | 久草免费福利 | 韩日精品一区 | 九九看片 | 欧美一区二区三区在线观看 | 国产h视频 | 永久av| 久久久久国产精品一区二区 | 91大神新作在线观看 | 四虎av电影 | 婷婷五月色综合香五月 | 激情久久网 | 国产97碰免费视频 | 91在线色视频 | 欧美精品乱码久久久久久按摩 | 免费观看日韩av | 综合第一页| 欧美在线视频网 | 中文字幕成人av | 国外成人在线视频 | 中文字幕亚洲视频 | 亚洲精品1区 | 精品欧美色视频网站在线观看 | 国产一区二区久久 | 国产精品一区二区久久 | 中文字幕影院 | 国产视频二区在线观看 | 日韩一区二区在线视频 | 久久综合一区二区 | 亚洲中国字幕 | 亚洲精品一 | av在线免费观看不卡 | 狠狠久久久 | 在线视频亚洲 | 亚洲国产精品va在线看黑人 | 蜜桃传媒一区二区 | 亚洲国产成人在线视频 | 精精国产xxxx视频在线播放 |