覆盖虚函数并更改其返回类型---依赖问题 [英] Overriding a virtual function and changing its return type --- dependency issues

查看:63
本文介绍了覆盖虚函数并更改其返回类型---依赖问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我遇到了烦人的问题。这是我想要做的事情的简单概念

。包含防护装置不会显示可读性。我希望这不是太混乱了。我不认为描述会像

那样清楚。


----- [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对象。


有没有办法在
前向声明?如果没有,有没有人对这个问题有什么建议?


谢谢。

解决方案



" ctor" < blhbalh @ .balh>在消息中写道

news:u4 ******************************** @ 4ax.com ...



我正在经历一个恼人的问题。这是我想要做的事情的简化概念。包含防护装置不会显示可读性。我希望这不要太混乱;我不认为描述会像一个例子那样明确。

----- [ParentA.h] ------
类ParentA {};

----- [ChildA.h] ------
#include" ParentA.h"
#include" ChildB.h" ;类ChildA:public ParentA {ChildB foo; };

----- [ParentB.h] ------
类ParentA;
类ParentB {
虚拟ParentA& getA()= 0;
};

----- [ChildB.h] ------
#include" ParentB.h"
class ChildA;
class ChildB:public ParentB {
ChildA& getA();
};

---




为什么不返回ParentA&来自ChildB :: getA(),就像你在
中生成的那样:ParentB :: getA()?你正在返回一个引用,所以多态性将在
中发挥作用,并且在返回的ParentA对象中对虚函数的任何调用都将正确执行它们的ChildA对应物。并且通过公开继承,你说ChildA是一个 ParentA,所以不应该有任何问题。这个想法不起作用的原因是什么?


-Howard


2006年3月8日星期三19:03:17格林尼治标准时间,霍华德 < al ***** @ hotmail.com>

写道:


" ctor" < blhbalh @ .balh>在消息中写道
新闻:u4 ******************************** @ 4ax.com ..。< blockquote class =post_quotes>

我正在经历一个恼人的问题。这是我想要做的事情的简化概念。包含防护装置不会显示可读性。我希望这不要太混乱;我不认为描述会像一个例子那样明确。

----- [ParentA.h] ------
类ParentA {};

----- [ChildA.h] ------
#include" ParentA.h"
#include" ChildB.h" ;类ChildA:public ParentA {ChildB foo; };

----- [ParentB.h] ------
类ParentA;
类ParentB {
虚拟ParentA& getA()= 0;
};

----- [ChildB.h] ------
#include" ParentB.h"
class ChildA;
class ChildB:public ParentB {
ChildA& getA();
};

---



为什么不返回ParentA&来自ChildB :: getA(),就像你在ParentB :: getA()中一样?你正在返回一个引用,所以多态性将会发挥作用,并且在返回的ParentA对象中对虚函数的任何调用都将正确执行它们的ChildA对应物。通过公开继承,你说ChildA是一个 ParentA,所以不应该有任何问题。这个想法不起作用的原因是什么?

-Howard




嗨霍华德,谢谢你的回复。不幸的是,因为ChildB :: getA()是一个只能被

ChildB本身调用的函数,所以它不适合我的

目的。 ChildB知道它应该得到一个ChildA

引用,因此它希望能够使用ChildA的扩展

接口。



" ctor" < blhbalh @ .balh>在消息中写道

news:c1 ******************************** @ 4ax.com ...

2006年3月8日星期三19:03:17 GMT,霍华德 < al ***** @ hotmail.com>
写道:


" ctor" < blhbalh @ .balh>在消息中写道
新闻:u4 ******************************** @ 4ax.com。 ..



我正在经历一个恼人的问题。这是我想要做的事情的简化概念。包含防护装置不会显示可读性。我希望这不要太混乱;我不认为描述会像一个例子那样明确。

----- [ParentA.h] ------
类ParentA {};

----- [ChildA.h] ------
#include" ParentA.h"
#include" ChildB.h" ;类ChildA:public ParentA {ChildB foo; };

----- [ParentB.h] ------
类ParentA;
类ParentB {
虚拟ParentA& getA()= 0;
};

----- [ChildB.h] ------
#include" ParentB.h"
class ChildA;
class ChildB:public ParentB {
ChildA& getA();
};

---



为什么不返回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 what
I am trying to do. Inclusion guards aren''t shown for readability. I
hope this isn''t too confusing; I don''t think a description would be as
clear 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 following
error message:

error C2555: ''ChildB::getA'': overriding virtual function return type
differs and is not covariant from ''ParentB::getA''

I took this to mean that the problem is that the compiler doesn''t
realize that ChildA is a type of ParentA because I forward-declared
ChildA instead of including its header file, so ChildB::getA looks
like an invalid signature. The problem is that if I replace the
forward-declaration with "#include ChildA.h", I encounter a dependency
issue, since ChildA.h also has to include ChildB.h because the ChildA
class contains a ChildB object.

Is there a way to specify that ChildA is a child of ParentA in a
forward-declaration? If not, does anyone have any suggestions for
getting around this problem?

Thanks.

解决方案


"ctor" <blhbalh@.balh> wrote in message
news:u4********************************@4ax.com...

Hi,

I''m experiencing an annoying issue. Here is a simplified idea of what
I am trying to do. Inclusion guards aren''t shown for readability. I
hope this isn''t too confusing; I don''t think a description would be as
clear 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();
};

---



Why not return a ParentA& from ChildB::getA(), just like you do in
ParentB::getA()? You''re returning a reference, so polymorphism will come
into play, and any calls to virtual functions in the returned ParentA object
will correctly execute their ChildA counterparts. And by publicly
inheriting, you''ve said that ChildA "is a" ParentA, so there shouldn''t be
any problem. Any reason this idea won''t work?

-Howard


On Wed, 08 Mar 2006 19:03:17 GMT, "Howard" <al*****@hotmail.com>
wrote:


"ctor" <blhbalh@.balh> wrote in message
news:u4********************************@4ax.com.. .

Hi,

I''m experiencing an annoying issue. Here is a simplified idea of what
I am trying to do. Inclusion guards aren''t shown for readability. I
hope this isn''t too confusing; I don''t think a description would be as
clear 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();
};

---



Why not return a ParentA& from ChildB::getA(), just like you do in
ParentB::getA()? You''re returning a reference, so polymorphism will come
into play, and any calls to virtual functions in the returned ParentA object
will correctly execute their ChildA counterparts. And by publicly
inheriting, you''ve said that ChildA "is a" ParentA, so there shouldn''t be
any 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.



"ctor" <blhbalh@.balh> wrote in message
news:c1********************************@4ax.com...

On Wed, 08 Mar 2006 19:03:17 GMT, "Howard" <al*****@hotmail.com>
wrote:


"ctor" <blhbalh@.balh> wrote in message
news:u4********************************@4ax.com. ..

Hi,

I''m experiencing an annoying issue. Here is a simplified idea of what
I am trying to do. Inclusion guards aren''t shown for readability. I
hope this isn''t too confusing; I don''t think a description would be as
clear 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();
};

---



Why not return a ParentA& from ChildB::getA(), just like you do in
ParentB::getA()? You''re returning a reference, so polymorphism will come
into play, and any calls to virtual functions in the returned ParentA
object
will correctly execute their ChildA counterparts. And by publicly
inheriting, you''ve said that ChildA "is a" ParentA, so there shouldn''t be
any 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 the
definition of getA(), but if it constructs (or otherwise obtains) a ChildA
object, then referring to it via a ParentA reference (or pointer) is
perfectly valid.

If you need to refer to members which exist ONLY in the ChildA object, then
you can use dynamic_cast on the returned reference where needed.

If you don''t want to return a ParentA& reference from getA(), then why did
you make it a virtual function in ParentB in the first place?

-Howard



这篇关于覆盖虚函数并更改其返回类型---依赖问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆