本文介绍了“带接口的C ++” (CUJ第22卷第9期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有趣的文章。然而,它从一个例子开始,我发现 相当误导。我希望作者(克里斯托弗迪金斯)如果我在文章中发表引文,就不会想到。我知道他可能会阅读或者 甚至写给clc ++。有时候。 克里斯托弗给出了这个[伪]代码(是的,它是'不是C ++): 界面IFuBar { 合约: 无效FuBar(); }; struct BaseFuBar { void FuBar(){std :: cout<< " BaseFuBar英寸; } }; struct DerivedFuBar:public BaseFuBar { void FuBar(){std :: cout<< " DerivedFuBar英寸; } }; main(){ DerivedFuBar d; BaseFuBar& b = d; IFuBar i = b; i.FuBar(); //输出BaseFuBar } 然后声称标准C ++中接近此设计的常用方法 是:" struct AbcFuBar { 虚拟无效FuBar()= 0; }; // vb:缺少#include< iostream> struct BaseFuBar:public AbcFuBar {// vb:public是不必要的 void FuBar(){std :: cout<< " BaseFuBar英寸; } }; struct DerivedFuBar:public BaseFuBar { void FuBar(){std :: cout<< " DerivedFuBar英寸; } }; main(){// vb:哪里是''int''? DerivedFuBar d; BaseFuBar& b = d; /// ************************ AbcFuBar& a = b; a.FuBar(); //输出DerivedFuBar } 嗯,简单的''public''缺失,缺少< iostream>和 absense''main''返回值类型除外,代码似乎合乎逻辑,是吗? 我通过更改标有 星号,所需的行为可以__rectly_实现: BaseFuBar b = d; 与作者所暗示的相反。 现在,我在想:我是否应该看完这篇文章的其余部分 看到了这个和 main(){ ... 在一个所谓的_standard_ C ++程序中? Christopher是Heron的开发者,另一种语言。我想知道, 我们真的需要另一种语言开发,因为有人没有足够的学习现有的语言吗?不,那不可能...... 无论如何... Victor Interesting article. However, it starts with an example which I findrather misleading. I hope the author (Christopher Diggins) wouldn''tmind if I post a quote from the article. I know he probably reads oreven writes to c.l.c++.m sometimes. Christopher gives this [pseudo-]code (yes, it''s not C++): interface IFuBar {contract:void FuBar();}; struct BaseFuBar {void FuBar() { std::cout << "BaseFuBar"; }}; struct DerivedFuBar : public BaseFuBar {void FuBar() { std::cout << "DerivedFuBar"; }}; main() {DerivedFuBar d;BaseFuBar& b = d;IFuBar i = b;i.FuBar(); // outputs BaseFuBar} and then claims that "the common way to approximate this designin standard C++ is:" struct AbcFuBar {virtual void FuBar() = 0;}; // vb: missing #include <iostream> struct BaseFuBar : public AbcFuBar { // vb: public is unnecessaryvoid FuBar() { std::cout << "BaseFuBar"; }}; struct DerivedFuBar : public BaseFuBar {void FuBar() { std::cout << "DerivedFuBar"; }}; main() { // vb: where is ''int''?DerivedFuBar d;BaseFuBar& b = d; /// ************************AbcFuBar& a = b;a.FuBar(); // output DerivedFuBar} Well, simple superfluosity of ''public'', the missing <iostream>, and theabsense of ''main'' return value type aside, the code seems logical, yes? I submit that by changing a single character on the line marked withasterisks, the desired behaviour can be _correctly_ implemented: BaseFuBar b = d; contrary to what the author implies. Now, I am thinking: should I really read the rest of the article afterseeing that and main() {... in a supposedly _standard_ C++ program? Christopher is a developer of Heron, another language. I am wondering,do we really need another language developed because somebody hasn''t gotaround to learning existing ones well enough? Nah, that can''t be it... Anyway... Victor推荐答案 Haven不读这篇文章(你有吗?一个链接?),但是: 期望的行为是什么? 根据你所写的内容来判断是否接到BaseFuBar的电话:: FuBar? 这很容易实现,如你所示;还有其他方法。 顺便说一句。你在clc ++中发帖,而不是clc ++ m ... ;-) - 答:因为它弄乱了订单人们通常会阅读文字。 问:为什么这么糟糕? A:热门帖子。 问:什么是usenet和电子邮件中最烦人的事情是? Haven''t read the article (do you have a link?), but: What is the desired behavior? Judging from what you write it is to get a call of BaseFuBar::FuBar? That''s easy to achieve as you''ve shown; there are also other ways. Btw. you''re posting in clc++, not clc++m... ;-) --A: Because it messes up the order in which people normally read text.Q: Why is it such a bad thing?A: Top-posting.Q: What is the most annoying thing on usenet and in e-mail? 使用Warp 9.9999进行虚拟调度速度和零空间开销。 中尉指挥官数据充满了这些东西。 问候, 亚历山大。 Virtual dispatch with Warp 9.9999 velocity and zero space overhead. Lieutenant Commander Data is full of such things. regards,alexander. 不,我没有链接。我刚收到杂志的副本。 期望的行为是什么? 正如我从伪代码中所理解的那样,它要使BaseFuBar子对象 来做它的事情,而不是DerivedFuBar。 根据你所写的内容来判断是否接到了BaseFuBar :: FuBar的电话? 我是这么认为的。 这很容易实现,如你所示;还有其他方法。 您应该发布您的版本。 顺便说一句。你发布的是clc ++,而不是clc ++ m ...; - ) No, I don''t have a link. I just have my copy of the magazine. What is the desired behavior?As I understood from the pseudo-code it''s to make the BaseFuBar subobjectto do its thing, but not the DerivedFuBar. Judging from what you write it is to get a call of BaseFuBar::FuBar?I thought so. That''s easy to achieve as you''ve shown; there are also other ways.You should post your versions. Btw. you''re posting in clc++, not clc++m... ;-) 我知道。我很少在c.l.c ++。中发帖。太符合我的口味了。 V I know. I rarely post in c.l.c++.m. Too ''m'' for my taste. V 这篇关于“带接口的C ++” (CUJ第22卷第9期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-24 11:42