pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

為什么 C++ 不讓基類實(shí)現(xiàn)派生類的繼承接口?

Why does C++ not let baseclasses implement a derived class#39; inherited interface?(為什么 C++ 不讓基類實(shí)現(xiàn)派生類的繼承接口?)
本文介紹了為什么 C++ 不讓基類實(shí)現(xiàn)派生類的繼承接口?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

這就是我要說的

// some guy wrote this, used as a Policy with templates
struct MyWriter {
  void write(std::vector<char> const& data) {
    // ...
  }
};

在現(xiàn)有的一些代碼中,人們沒有使用模板,而是使用接口+類型擦除

In some existing code, the people did not use templates, but interfaces+type-erasure

class IWriter {
public:
  virtual ~IWriter() {}

public:
  virtual void write(std::vector<char> const& data) = 0;
};

其他人想要同時(shí)使用方法和寫入

Someone else wanted to be usable with both approaches and writes

class MyOwnClass: private MyWriter, public IWriter {
  // other stuff
};

MyOwnClass 是根據(jù) MyWriter 實(shí)現(xiàn)的.為什么MyOwnClass的繼承成員函數(shù)沒有自動(dòng)實(shí)現(xiàn)IWriter的接口?相反,用戶必須編寫只調(diào)用基類版本的轉(zhuǎn)發(fā)函數(shù),如

MyOwnClass is implemented-in-terms-of MyWriter. Why doesn't MyOwnClass' inherited member functions implement the interface of IWriter automatically? Instead the user has to write forwarding functions that do nothing but call the base class versions, as in

class MyOwnClass: private MyWriter, public IWriter {
public:
  void write(std::vector<char> const& data) {
    MyWriter::write(data);
  }
};

我知道在 Java 中,當(dāng)您有一個(gè)實(shí)現(xiàn)接口并從恰好具有合適方法的類派生的類時(shí),該基類會(huì)自動(dòng)實(shí)現(xiàn)派生類的接口.

I know that in Java when you have a class that implements an interface and derives from a class that happens to have suitable methods, that base class automatically implements the interface for the derived class.

為什么 C++ 不這樣做?擁有這似乎是一件很自然的事情.

Why doesn't C++ do that? It seems like a natural thing to have.

推薦答案

這是多重繼承,有兩個(gè)繼承的函數(shù),簽名相同,都有實(shí)現(xiàn).這就是 C++ 與 Java 不同的地方.

This is multiple inheritance, and there are two inherited functions with the same signature, both of which have implementation. That's where C++ is different from Java.

在靜態(tài)類型為 MyBigClass 的表達(dá)式上調(diào)用 write 因此對于需要哪個(gè)繼承函數(shù)是不明確的.

Calling write on an expression whose static type is MyBigClass would therefore be ambiguous as to which of the inherited functions was desired.

如果 write 僅通過基類指針調(diào)用,則不需要在派生類中定義 write,這與問題中的聲明相反. 既然問題已更改為包含純說明符,那么在派生類中實(shí)現(xiàn)該函數(shù)對于使類具體化和可實(shí)例化是必要的.

If write is only called through base class pointers, then defining write in the derived class is NOT necessary, contrary to the claim in the question. Now that the question changed to include a pure specifier, implementing that function in the derived class is necessary to make the class concrete and instantiable.

MyWriter::write 不能用于 MyBigClass 的虛調(diào)用機(jī)制,因?yàn)樘撜{(diào)用機(jī)制需要一個(gè)接受隱式 IWriter* const 的函數(shù)thisMyWriter::write 接受一個(gè)隱式的 MyWriter* const this.需要一個(gè)新函數(shù),必須考慮到IWriter子對象和MyWriter子對象的地址差異.

MyWriter::write cannot be used for the virtual call mechanism of MyBigClass, because the virtual call mechanism requires a function that accepts an implicit IWriter* const this, and MyWriter::write accepts an implicit MyWriter* const this. A new function is required, which must take into account the address difference between the IWriter subobject and the MyWriter subobject.

編譯器自動(dòng)創(chuàng)建這個(gè)新函數(shù)在理論上是可能的,但它很脆弱,因?yàn)榛惖淖兓赡軙?huì)突然導(dǎo)致選擇一個(gè)新函數(shù)進(jìn)行轉(zhuǎn)發(fā).它在 Java 中不那么脆弱,只有單繼承是可能的(對于要轉(zhuǎn)發(fā)到哪個(gè)函數(shù)只有一種選擇),但是在支持完全多重繼承的 C++ 中,選擇是不明確的,我們甚至還沒有開始鉆石繼承或虛擬繼承.

It would be theoretically possible for the compiler to create this new function automatically, but it would be fragile, since a change in a base class could suddenly cause a new function to be chosen for forwarding. It's less fragile in Java, where only single inheritance is possible (there's only one choice for what function to forward to), but in C++, which supports full multiple inheritance, the choice is ambiguous, and we haven't even started on diamond inheritance or virtual inheritance yet.

其實(shí)這個(gè)問題(子對象地址之間的差異)是通過虛擬繼承解決的.但它需要額外的開銷,這在大多數(shù)情況下是不必要的,而 C++ 的指導(dǎo)原則是不用為不使用的東西付費(fèi)".

Actually, this problem (difference between subobject addresses) is solved for virtual inheritance. But it requires additional overhead that's not necessary most of the time, and a C++ guiding principle is "you don't pay for what you don't use".

這篇關(guān)于為什么 C++ 不讓基類實(shí)現(xiàn)派生類的繼承接口?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 户外健身路径_小区健身器材_室外健身器材厂家_价格-浩然体育 | 恒温振荡混匀器-微孔板振荡器厂家-多管涡旋混匀器厂家-合肥艾本森(www.17world.net) | 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 山东集装箱活动房|济南集装箱活动房-济南利森集装箱有限公司 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 阻垢剂,反渗透阻垢剂,缓蚀阻垢剂-山东普尼奥水处理科技有限公司 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | Dataforth隔离信号调理模块-信号放大模块-加速度振动传感器-北京康泰电子有限公司 | 中国产业发展研究网 - 提供行业研究报告 可行性研究报告 投资咨询 市场调研服务 | 特材真空腔体_哈氏合金/镍基合金/纯镍腔体-无锡国德机械制造有限公司 | 深圳APP开发_手机软件APP定制外包_小程序开发公司-来科信 | 深圳成考网-深圳成人高考报名网| 北京中创汇安科贸有限公司 | 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 包装设计公司,产品包装设计|包装制作,包装盒定制厂家-汇包装【官方网站】 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | 管理会计网-PCMA初级管理会计,中级管理会计考试网站 | 交通信号灯生产厂家_红绿灯厂家_电子警察监控杆_标志杆厂家-沃霖电子科技 | 干式磁选机_湿式磁选机_粉体除铁器-潍坊国铭矿山设备有限公司 | CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | LNG鹤管_内浮盘价格,上装鹤管,装车撬厂家-连云港赛威特机械 | 塑胶跑道施工-硅pu篮球场施工-塑胶网球场建造-丙烯酸球场材料厂家-奥茵 | 真空干燥烘箱_鼓风干燥箱 _高低温恒温恒湿试验箱_光照二氧化碳恒温培养箱-上海航佩仪器 | 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 洁净化验室净化工程_成都实验室装修设计施工_四川华锐净化公司 | 沈阳激光机-沈阳喷码机-沈阳光纤激光打标机-沈阳co2激光打标机 | 河南生物显微镜,全自动冰冻切片机-河南荣程联合科技有限公司 | 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 酒万铺-酒水招商-酒水代理| 卫浴散热器,卫浴暖气片,卫生间背篓暖气片,华圣格浴室暖气片 | 软文世界-软文推广-软文营销-新闻稿发布-一站式软文自助发稿平台 | 兰州UPS电源,兰州山特UPS-兰州万胜商贸 | 行业分析:提及郑州火车站附近真有 特殊按摩 ?2025实地踩坑指南 新手如何避坑不踩雷 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 旋转滴界面张力仪(张力测定仪器)-百科| 直读光谱仪,光谱分析仪,手持式光谱仪,碳硫分析仪,创想仪器官网 | 高柔性拖链电缆-聚氨酯卷筒电缆-柔性屏蔽电缆厂家-玖泰电缆 | 定做大型恒温循环水浴槽-工业用不锈钢恒温水箱-大容量低温恒温水槽-常州精达仪器 | 学考网学历中心|