简体   繁体   English

C ++虚拟方法:是否必须在父类中为子级和父级不共享的每个方法创建一个虚拟方法?

[英]C++ Virtual Methods: Must I create a virtual method in the parent class for every method that the child and parent do not share?

I have several objects of a Polygon type that just define shapes and I want to store them in a vector that holds Polygons. 我有几个仅定义形状的Polygon类型的对象,我想将它们存储在保存Polygons的向量中。 Polygon has several general methods like getArea() and such, but it is a purely abstract class. 多边形有几种通用方法,例如getArea()等,但它是一个纯粹的抽象类。

If I have a child of Polygon called Circle, with the method getRadius(), do I need to create a virtual method in Polygon to access that method if it is stored in a vector? 如果我有一个名为Polygon的Polygon子元素,使用方法getRadius(),是否需要在Polygon中创建一个虚拟方法来访问该方法(如果该方法存储在向量中)?

If I expand to more and more shapes, wouldn't creating all those virtual methods in the Polygon declaration (with the associating child method in the child's declaration) be redundant and a waste of space? 如果我将形状扩展到越来越多的形状,那么在Polygon声明中创建所有这些虚拟方法(在子声明中使用关联的子方法)是否会多余并且浪费空间?

Is there another way to inform the compiler that the object I created (of child type) in the Polygon vector of the available child methods without using virtual methods? 有没有其他方法可以通知编译器在多边形向量中我创建的对象(子类型)可用的子方法,而无需使用虚拟方法? Or was that kind of the whole point of virtual methods? 还是虚拟方法的全部重点?

I understand how to use virtual methods that the child and parent share, I'm just asking for situations where the parent does not have the method I want to access from the child. 我了解如何使用子级和父级共享的虚拟方法,我只是在询问父级没有要从子级访问的方法的情况。

The point of virtual methods is to allow methods which are implemented differently by different children to be called at runtime with the correct implementation. 虚拟方法的重点是允许在运行时使用正确的实现调用由不同子代以不同方式实现的方法。

If you have a method that is specific to one child class only, then the parent should not have a virtual method for it. 如果您有特定于只有一个子类的方法,那么,父母都不该有这方面的虚方法。

If you are trying to call methods which are specific to Circle then you should be using Circle* instead of Polygon* . 如果尝试调用特定于Circle方法,则应使用Circle*而不是Polygon*

If the not-shared method does not make sense for the parent class, then you do not want to put it in the parent class at all. 如果非共享方法对父类没有意义,那么您根本就不想将其放在父类中。 In fact, in that case, polymorphism does not apply, because you cannot have different runtime instances behaving differently on that method: that method just does not make sense to the instances of some child classes, while it does for some others. 实际上,在那种情况下,多态性不适用,因为您不能在该方法上使用不同的运行时实例,这些实例的行为也有所不同:该方法对某些子类的实例没有意义,而对其他子类的实例则有意义。 Therefore, you may want to create a new class, which (1) will become a child of the previously mentioned parent class, (2) will have a virtual method that is shared among the other classes, (3) will be extended by those classes. 因此,您可能想创建一个新类,该类(1)将成为前面提到的父类的子类,(2)具有一个在其他类之间共享的虚拟方法,(3)将被那些类扩展类。

In general, if you want to invoke a method specific for a particular class, you need to cast your object to that class or to one of its ancestor declaring that method (virtual or not). 通常,如果要调用特定于特定类的方法,则需要将对象强制转换为该类或其声明该方法的祖先之一(无论是否为虚拟)。

I suppose you want to make a single getRadius call to a vector which will call individual polygons respectively. 我想您想对向量进行单个getRadius调用,该向量将分别调用各个多边形。 Then a base class obviously makes sense. 然后,一个基类显然是有意义的。

Just implement the individual GetRadius Method for all the childclasses and have one default virtual function to ensure consistency. 只需为所有子类实现单独的GetRadius方法,并具有一个默认的虚函数即可确保一致性。

Since, you're saying, there are multiple shapes, it implies there are multiple classes aka multiple GetRadius implementations. 因为您要说的是有多种形状,这意味着有多个类,也就是多个GetRadius实现。

A virtual function will simplify your calling process. 虚函数将简化您的调用过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM