本文介绍了覆盖虚函数并更改其返回类型---依赖问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我遇到了烦人的问题。这是我想要做的事情的简单概念 。包含防护装置不会显示可读性。我希望这不是太混乱了。我不认为描述会像 那样清楚。 ----- [ParentA.h] ---- - class ParentA {}; ----- [ChildA.h] ------ #include" ParentA.h" #include" ChildB.h" class ChildA:public ParentA {ChildB foo; }; ----- [ParentB.h] ------ class ParentA; class ParentB { 虚拟ParentA& getA()= 0; }; ----- [ChildB.h] ------ #include" ParentB.h" class ChildA; class ChildB:public ParentB { ChildA& getA(); }; --- 当我尝试编译我的项目时,MSVC 2002给我以下 错误信息: 错误C2555:''ChildB :: getA'':覆盖虚函数返回类型 与''ParentB :: getA'不同并且不是协变的。 我认为问题在于编译器没有$ / 意识到ChildA是ParentA的一种类型,因为我转发声明了 ChildA而不是包含它的头文件,所以ChildB :: getA看起来像b / b 一样无效的签名。问题是,如果我用'#include ChildA.h'替换 前向声明,我会遇到一个依赖 问题,因为ChildA.h也必须包含ChildB.h,因为ChildA 类包含一个ChildB对象。 有没有办法在 前向声明?如果没有,有没有人对这个问题有什么建议? 谢谢。 解决方案 为什么不返回ParentA&来自ChildB :: getA(),就像你在中生成的那样:ParentB :: getA()?你正在返回一个引用,所以多态性将在中发挥作用,并且在返回的ParentA对象中对虚函数的任何调用都将正确执行它们的ChildA对应物。并且通过公开继承,你说ChildA是一个 ParentA,所以不应该有任何问题。这个想法不起作用的原因是什么? -Howard 为什么不返回ParentA&来自ChildB :: getA(),就像你在ParentB :: getA()中一样?你正在返回一个引用,所以多态性将会发挥作用,并且在返回的ParentA对象中对虚函数的任何调用都将正确执行它们的ChildA对应物。通过公开继承,你说ChildA是一个 ParentA,所以不应该有任何问题。这个想法不起作用的原因是什么? -Howard 嗨霍华德,谢谢你的回复。不幸的是,因为ChildB :: getA()是一个只能被 ChildB本身调用的函数,所以它不适合我的 目的。 ChildB知道它应该得到一个ChildA 引用,因此它希望能够使用ChildA的扩展 接口。 为什么不返回ParentA&来自ChildB :: getA(),就像你在ParentB :: getA()中一样?你正在返回一个引用,所以多态性将会发挥作用,并且在返回的ParentA 对象中对虚函数的任何调用都将正确执行它们的ChildA对应物。通过公开继承,你说ChildA是一个 ParentA,所以不应该有任何问题。这个想法不起作用的原因是什么? -Howard 嗨霍华德,谢谢你的回复。不幸的是,这不适合我的目的,因为ChildB :: getA()是一个只能由ChildB本身调用的函数。 ChildB知道应该获得ChildA 参考,因此它希望能够使用ChildA的扩展接口。 但它可以!这就是多态性的全部意义所在。你没有显示getA()的 定义,但如果它构造(或以其他方式获得)一个ChildA 对象,那么通过ParentA引用引用它(或指针) 完全有效。 如果你需要引用仅存在于ChildA对象中的成员,那么 你可以在需要的地方使用返回的引用上的dynamic_cast。 如果你不想退回ParentA&来自getA()的引用,那么为什么 首先让它成为ParentB中的虚函数? -Howard Hi, I''m experiencing an annoying issue. Here is a simplified idea of whatI am trying to do. Inclusion guards aren''t shown for readability. Ihope this isn''t too confusing; I don''t think a description would be asclear as an example. -----[ParentA.h]------class ParentA {}; -----[ChildA.h]------#include "ParentA.h"#include "ChildB.h"class ChildA: public ParentA { ChildB foo; }; -----[ParentB.h]------class ParentA;class ParentB {virtual ParentA& getA() = 0;}; -----[ChildB.h]------#include "ParentB.h"class ChildA;class ChildB: public ParentB {ChildA& getA();}; --- When I try to compile my project, MSVC 2002 gives me the followingerror message: error C2555: ''ChildB::getA'': overriding virtual function return typediffers and is not covariant from ''ParentB::getA'' I took this to mean that the problem is that the compiler doesn''trealize that ChildA is a type of ParentA because I forward-declaredChildA instead of including its header file, so ChildB::getA lookslike an invalid signature. The problem is that if I replace theforward-declaration with "#include ChildA.h", I encounter a dependencyissue, since ChildA.h also has to include ChildB.h because the ChildAclass contains a ChildB object. Is there a way to specify that ChildA is a child of ParentA in aforward-declaration? If not, does anyone have any suggestions forgetting around this problem? Thanks. 解决方案 Why not return a ParentA& from ChildB::getA(), just like you do inParentB::getA()? You''re returning a reference, so polymorphism will comeinto play, and any calls to virtual functions in the returned ParentA objectwill correctly execute their ChildA counterparts. And by publiclyinheriting, you''ve said that ChildA "is a" ParentA, so there shouldn''t beany problem. Any reason this idea won''t work? -HowardWhy not return a ParentA& from ChildB::getA(), just like you do inParentB::getA()? You''re returning a reference, so polymorphism will comeinto play, and any calls to virtual functions in the returned ParentA objectwill correctly execute their ChildA counterparts. And by publiclyinheriting, you''ve said that ChildA "is a" ParentA, so there shouldn''t beany problem. Any reason this idea won''t work?-Howard Hi Howard, thanks for the response. Unfortunately that won''t suit mypurposes because ChildB::getA() is a function that only gets called byChildB itself. ChildB knows that it should be getting a ChildAreference, and so it expects to be able to use ChildA''s extendedinterface.Why not return a ParentA& from ChildB::getA(), just like you do inParentB::getA()? You''re returning a reference, so polymorphism will comeinto play, and any calls to virtual functions in the returned ParentAobjectwill correctly execute their ChildA counterparts. And by publiclyinheriting, you''ve said that ChildA "is a" ParentA, so there shouldn''t beany problem. Any reason this idea won''t work?-Howard Hi Howard, thanks for the response. Unfortunately that won''t suit my purposes because ChildB::getA() is a function that only gets called by ChildB itself. ChildB knows that it should be getting a ChildA reference, and so it expects to be able to use ChildA''s extended interface. But it can! That''s what polymorphism is all about. You don''t show thedefinition of getA(), but if it constructs (or otherwise obtains) a ChildAobject, then referring to it via a ParentA reference (or pointer) isperfectly valid. If you need to refer to members which exist ONLY in the ChildA object, thenyou can use dynamic_cast on the returned reference where needed. If you don''t want to return a ParentA& reference from getA(), then why didyou make it a virtual function in ParentB in the first place? -Howard 这篇关于覆盖虚函数并更改其返回类型---依赖问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-10 12:46