在由基类指针指向时执行子方法 [英] executing a child method when pointed to by a base class pointer

查看:61
本文介绍了在由基类指针指向时执行子方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在基类中定义一个方法,使得派生的

类将调用自己的版本,即使派生的实例是由

引用的指向基类的指针?请注意,基类方法

不是抽象的,如果实例是作为

''泛型''基类实例创建的,则会调用它。 />

这有点像我希望这个方法对孩子们来说是抽象的,但具体的是基础层面的
。这样我就可以引用一个指针数组来基础

类元素,当要求执行此方法时,每个元素都会调用它的子方法版本的方法(没有被告知(即,施放)它是'/ b $ b''儿童类''类型)。


这可能吗?我可以创建一个抽象类,将此方法作为

abstract并从中派生出''基类''和''子类'',其中

case指向包含基类实例或

子类的抽象类的指针将执行approriate方法。但后来''儿童班''

不再是基类的孩子了。我宁愿这样做,就像我描述的那样...... / b $ b以上......


感谢您的回复! :)


[== P ==]

Is there a way of defining a method in a base class such that derived
classes will call their own version, EVEN if the derived instance is
referred to by a pointer to the base class? Note that the base class method
is not to be abstract, and will be called if the instance was created as a
''generic'' base class instance.

It''s sort of like I want the method to be abstract to children, but concrete
at the base level. That way I can refer to an array of pointers to base
class elements that each will call its child''s version of the method when
asked to execute this method (without being told (i.e., cast to) it''s of
''child class'' type).

Is this possible? I could create an abstract class that has this method as
abstract and derive both ''base class'' and ''child class'' from it, in which
case a pointer to the abstract class that holds an instance of the base or
child class will execute the approriate method. But then the ''child class''
is no longer a child of the ''base class''. I''d rather do it like I described
above...

Thanks in avance for responses! : )

[==P==]

推荐答案

Peter Oliphant写道:
Peter Oliphant wrote:
是否有一种方法可以在基类中定义一个方法,使派生的类可以调用自己的版本,即使派生实例被指针引用也是如此。基类?请注意,基类方法不是抽象的,如果实例是作为通用基类实例创建的,则会调用它。

它'有点像我希望这个方法对孩子们来说是抽象的,但在基层是具体的。这样我就可以引用一个指向数组的指针数组,当要求执行这个方法时,每个元素都会调用它的子方法版本(没有被告知(即,转换为这是'/'''儿童班''类型)。

这可能吗?我可以创建一个抽象类,它将此方法抽象为抽象,并从中派生出基类和子类,其中
是一个指向抽象类的指针基类或
子类的实例将执行approriate方法。但随后儿童班不再是基类的孩子了。我宁愿这样做,就像上面描述的那样......

感谢您的回复! :)

[== P ==]
Is there a way of defining a method in a base class such that derived
classes will call their own version, EVEN if the derived instance is
referred to by a pointer to the base class? Note that the base class method
is not to be abstract, and will be called if the instance was created as a
''generic'' base class instance.

It''s sort of like I want the method to be abstract to children, but concrete
at the base level. That way I can refer to an array of pointers to base
class elements that each will call its child''s version of the method when
asked to execute this method (without being told (i.e., cast to) it''s of
''child class'' type).

Is this possible? I could create an abstract class that has this method as
abstract and derive both ''base class'' and ''child class'' from it, in which
case a pointer to the abstract class that holds an instance of the base or
child class will execute the approriate method. But then the ''child class''
is no longer a child of the ''base class''. I''d rather do it like I described
above...

Thanks in avance for responses! : )

[==P==]



彼得:


也许我错过了什么,但是你为什么不把这个方法变成虚拟的并且在基类中有一个实现。或者,如果你不是那样的b $ b,那就做一个新的抽象课并做一下


摘要 - >基地 - >派生


BTW,恕我直言,使用术语子类并不好。使用衍生的

类或子类。孩子经常有另一种含义,比如在Windows中使用

亲子。

David Wilkinson


Peter:

Maybe I''m missing something, but why don''t you just make the method
virtual and have an implementation in the base class. Or if you don''t
like that, then make a new abstract class and do

abstract -> base -> derived

BTW, IMHO it is not good to use the term "child class". Use "derived
class" or "sub-class". Child often has another meaning, like
parent-child in Windows.

David Wilkinson

" Peter Oliphant" < PO ******* @ RoundTripInc.com>在消息中写道

news:e
"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:e


************** @ TK2MSFTNGP11.phx.gbl ...
**************@TK2MSFTNGP11.phx.gbl...
是否有一种方法可以在基类中定义一个方法,使派生的类可以调用自己的版本,即使派生实例被指向基类的指针引用也是如此类?请注意,基类
方法不是抽象的,如果实例被创建为通用基类实例,则会调用它。

它'有点像我希望这个方法对孩子们来说是抽象的,但是在基层是具体的。这样我就可以引用一个指针数组来基类元素,当被要求执行这个方法时,每个元素都会调用它的孩子的
方法版本(没有被告知(即,施放<它是''儿童类''类型)。

这可能吗?我可以创建一个抽象类,它将此方法抽象为抽象,并从中派生出基类和子类,其中
是一个指向抽象类的指针基类或
子类的实例将执行approriate方法。但随后儿童班不再是基类的孩子了。我宁愿像上面描述的那样做...
Is there a way of defining a method in a base class such that derived
classes will call their own version, EVEN if the derived instance is
referred to by a pointer to the base class? Note that the base class
method is not to be abstract, and will be called if the instance was
created as a ''generic'' base class instance.

It''s sort of like I want the method to be abstract to children, but
concrete at the base level. That way I can refer to an array of pointers
to base class elements that each will call its child''s version of the
method when asked to execute this method (without being told (i.e., cast
to) it''s of ''child class'' type).

Is this possible? I could create an abstract class that has this method as
abstract and derive both ''base class'' and ''child class'' from it, in which
case a pointer to the abstract class that holds an instance of the base or
child class will execute the approriate method. But then the ''child class''
is no longer a child of the ''base class''. I''d rather do it like I
described above...




这只是基本的多态性,由两者中的虚函数实现>
本机C ++和.NET。


只需在基类中声明虚函数并在

派生类中重写它。通过指针(或引用)调用基类

实际上会调用派生类提供的实现。


如果你不是想在基类中实现一个实现,在本机C ++中声明它是纯粹的

(= 0),或者在.NET中声明它,否则你将不得不提供一个

基类中函数的定义。


-cd



This is just basic polymorphism, as implemented by virtual functions in both
native C++ and .NET.

Simply declare the function virtual in the base class and override it in the
derived class(es). Calls through a pointer (or reference) to the base class
will in fact call the implementation supplied by the derived class.

If you don''t want an implementation in the base class, declare it "pure"
(=0) in native C++, or abstract in .NET, otherwise you''ll have to supply a
definition of the function in the base class as well.

-cd


这篇关于在由基类指针指向时执行子方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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