如何知道函数何时必须是虚函数?
在描述类时,如何知道函数何时必须是虚拟的?
我知道虚拟函数是什么意思,但我只是不知道什么时候应该将它们设为虚拟
谢谢
While describing a class, how to know when function has to be virtual?
I know what virtual function means, but I just can't figure out when I should make them virtual
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想多态调用函数,则函数应该是虚拟的。换句话说,虚函数表达了可以在子类中自定义的行为。例如,考虑以下类:
每辆车都有一个
Id
,并且这里不允许自定义,因此没有必要将其设为虚拟。子类不应该能够修改该属性。然而,发动机如何启动取决于具体的汽车,并且实际上不能全局定义。但我们确实知道,每辆车都必须启动引擎才能行驶,所以我们将其定义为纯虚函数。Functions should be virtual if you want to invoke them polymorphically. In other words, virtual function express behavior that can be customized in subclasses. For instance, consider the following class:
Every car has an
Id
, and there is no customization allowed here, so there is no point in making it virtual. Subclasses should not be able to modify that property. How the engine is started however, depends on the specific car, and can in fact not be defined globally. But we do know that every car has to start the engine before we can drive, so we define it as a pure virtual function.An important guideline for when to make a function virtual, and when not, is given by the Non-Virtual Interface idiom.
您的类是否将具有虚函数取决于该类是否将用作基类,而这应该取决于设计和体系结构。
Whether your class is going to have virtual functions depends on whether the class is going to be used as a base class, and that should depend on the design and architecture.
这取决于派生类是否有可能对基类中存在的成员函数具有不同的功能。采用Polygon类的设计——
成员函数
Polygon::area()
需要是虚拟的。为什么 ?因为所有多边形的面积计算并不相同。类似地 -
请注意,正方形、矩形是多边形的一种类型,但两者的面积计算并不相同。在这种设计情况下,如果成员函数在派生类中可能有不同的实现,那么它在基类中就需要虚拟。
It depends on whether there is a possibility that the derived classes can have a different functionality for the member function present in base class. Taking the design of a Polygon class -
member function
Polygon::area()
needs to be virutal. Why ? Because the calculation of area for all the polygon isn't the same.Similarly -
Notice that Square, Rectangle are a type of Polygon but the calculation of area is not the same for both. In such cases of design, where a member function has the possibility of different implementation in derived class, it needs to be virtual in the base class.
如果您想通过基类的指针或引用来使用对象,则需要将函数设为虚拟。然后,您可以调用派生类的函数,而无需确切知道它是什么类型。
如果单独使用每个类,并且知道每个对象的类型,即使使用派生类,也不需要虚函数。
You need to make functions virtual if you want to use your objects through a pointer or reference to a base class. You can then call the derived class' function, without knowing exactly what type it is.
If you use each class separately, and know the type of each object, you don't need virtual functions even if you use derived classes.
在软件设计方面,如果您尝试定义抽象类或接口,您将使用 virtual 关键字来强制动态多态性。
In terms of Software Design, if you are trying to define and Abstract Class or Interface, you will be using virtual keyword to enforce dynamic polymorphism.
当您希望您的类被视为其他类的基类,并且您认为您的类中的某些函数可能被派生类覆盖时,那么很明显您应该将这些函数设为虚拟函数代码>.使它们成为虚拟的可以实现运行时多态性!这意味着,您可以多态地使用类,即使用类型基类的指针/引用,但指向派生类对象!
简而言之,在基于接口的类设计中,应该使函数虚拟化。
永远记住一件事:在设计多态类时一定不要忘记使基类析构函数
虚拟
!When you want your class to be treated as base class of some other class, and you think that there are some functions in your class which the derived classes might override, then it's good indication that you should make these functions
virtual
. Making themvirtual
enables runtime polymorphism! That means, you can use the classes polymorphically, i.e using pointer/reference of type base class, but pointing to derived class object!In short, in interface based class design, one should make functions
virtual
.Always remember one thing: Must not forget to make the base class destructor
virtual
when designing polymorphic classes!当您知道从该类继承的类中该方法的行为可能或将要更改时,应该将该方法设为虚拟方法。更准确地说,如果您希望子类覆盖该方法,则将其设为虚拟。如果您不想在基类中实现该方法,则将其设为纯虚拟:
即:
这样您的基类无法实例化(您可能不希望实例化它,因为它仍然有未实现的方法)。
使用虚拟方法,您可以让类多态性为您工作。
如果您知道某个方法的行为不会改变,那么就不要将其设为虚拟(查找虚拟函数调用比“最终”/非虚拟函数调用花费更长的时间)。
You should make the method virtual when you know that the method's behavior may or is going to change in classes inheriting from that class. More precisely, if you want a subclass to overwrite that method, then make it virtual. If you do not want to implement that method in your base class, then make it pure virtual:
I.e.:
This way your base class cannot be instantiated (you may not want it instantiated as it still has unimplemented methods left).
Using virtual methods you can have class polymorphism working for you.
If you know that a certain method is not going to change in behavior, then don't make it virtual (looking up virtual function calls takes longer than "final"/non-virtual function calls).