为什么虚拟功能不能过度使用? [英] Why should virtual functions not be used excessively?

查看:136
本文介绍了为什么虚拟功能不能过度使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读过,我们不应该过度使用虚拟函数。

I just read that we should not use virtual function excessively. People felt that less virtual functions tends to have fewer bugs and reduces maintenance.

虚拟函数会出现什么样的错误和缺点?

What kind of bugs and disadvantages can appear due to virtual functions?

我对C ++或Java的上下文感兴趣。

I'm interested in context of C++ or Java.

推荐答案

您发布了一些总括声明我认为最务实的程序员会因为误解或误解而耸耸肩。但是,确实存在反虚的狂热分子,他们的代码对性能和维护来说也是同样糟糕。

You've posted some blanket statements that I would think most pragmatic programmers would shrug off as being misinformed or misinterpreted. But, there do exist anti-virtual zealots, and their code can be just as bad for performance and maintenance.

在Java中,一切都是虚拟的。

In Java, everything is virtual by default. Saying you shouldn't use virtual functions excessively is pretty strong.

在C ++中,你必须声明一个函数virtual,但在适当的时候使用它是完全可以接受的。 p>

In C++, you must declare a function virtual, but it's perfectly acceptable to use them when appropriate.


我刚才读到我们不应该过度使用虚函数。

I just read that we should not use virtual function excessively.

很难定义过度...肯定适当时使用虚拟函数是好的建议。

It's hard to define "excessively"... certainly "use virtual functions when appropriate" is good advice.


认为较少的虚拟功能往往具有较少的错误和减少维护。
我无法得到虚拟函数会出现什么样的错误和缺点。

People felt that less virtual functions tends to have fewer bugs and reduces maintenance. I'm not able to get what kind of bugs and disadvantages can appear due to virtual functions.

设计不良的代码很难维护。

Poorly designed code is hard to maintain. Period.

如果你是一个库维护者,调试代码埋在一个高级的层次结构,可能很难跟踪代码实际执行的地方,的强大的IDE,通常很难知道哪个类覆盖行为。它可能导致在跟踪继承树的文件之间跳跃很多。

If you're a library maintainer, debugging code buried in a tall class hierarchy, it can be difficult to trace where code is actually being executed, without the benefit of a powerful IDE, it's often hard to tell just which class overrides the behavior. It can lead to a lot of jumping around between files tracing inheritance trees.

因此,有一些经验规则,除了例外:

So, there are some rules of thumb, all with exceptions:


  • 保持层次结构浅。

  • 在c ++中,如果您的类有虚函数,请使用虚析器(如果没有,可能是一个错误)

  • 与任何层次结构一样,保持派生类和基类之间的is-a关系。

  • 您必须注意,虚拟函数可能根本不被调用。因此,不要添加隐性预期。

  • 有一个难以辩驳的情况下,虚拟函数会更慢。它是动态绑定的,所以经常这样。它在大多数情况下是否重要,它的引用当然是有争议的。 Profile and optimize instead:)

  • 在C ++中,不需要使用虚拟时。在标记一个函数virtual时涉及到语义意义 - 不要滥用它。让读者知道是的,这可能被覆盖!。

  • 首选纯混合实现的层次结构的虚拟接口。

  • Keep your hierarchies shallow. Tall trees make for confusing classes.
  • In c++, if your class has virtual functions, use a virtual destructor (if not, it's probably a bug)
  • As with any hierarchy, keep to a 'is-a' relationship between derived and base classes.
  • You have to be aware, that a virtual function may not be called at all... so don't add implicit expectations.
  • There's a hard-to-argue case to be made that virtual functions are slower. It's dynamically bound, so it's often the case. Whether it matters in most of the cases that its cited is certainly debatable. Profile and optimize instead :)
  • In C++, don't use virtual when it's not needed. There's semantic meaning involved in marking a function virtual - don't abuse it. Let the reader know that "yes, this may be overridden!".
  • Prefer pure virtual interfaces to a hierarchy that mixes implementation. It's cleaner and much easier to understand.

现实的情况是,虚拟函数是非常有用的,这些阴影不可能来自平衡源 - 虚拟函数已经被广泛使用了很长时间。更多新的语言采用它们作为默认值。

The reality of the situation is that virtual functions are incredibly useful, and these shades of doubt are unlikely coming from balanced sources - virtual functions have been widely used for a very long time. More newer languages are adopting them as the default than otherwise.

这篇关于为什么虚拟功能不能过度使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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