java - Why should virtual functions not be used excessively? -


I have just read that we should not make excessive use of virtual functions; people thought less virtual functions were less insects. And maintenance decreases.

What kind of worms and disadvantages can be seen due to the virtual function?

I'm interested in C ++ or Java.


I can think that one reason the virtual function can be slower than normal work due to v-table lookup.

You have posted some blanket details, which I would think the most practical programmer is wrong or wrong Explain incorrectly. However, there are virtual virtual experts present, and their code may be as bad for performance and maintenance.

In Java, when you say everything is virtual by default, you should not make excessive use of the virtual function. It's very strong.

In C ++, you have to declare the function virtual, but it is perfectly acceptable to use them when appropriate.

I just read that we should not use the virtual function more.

It is difficult to define "important" ... Surely, "Use virtual functions when appropriate" is good advice.

People believe that less virtual functions have fewer worms and maintenance decreases, I can understand the type of worms and damage caused by the virtual function. I'm not able.

Bad design code is hard to maintain. Period.

If you are a library manager, for debugging codes buried in a high class hierarchy, it can be difficult to find out why the code is really being implemented, the powerful IDE Without the benefit of this, it is often difficult to tell which class has overridden the behavior, this legacy tree can grow for many jumps between tracing files.

Therefore, there are some rules of thumb, with all the exceptions:

  • Keep your hierarchy shallow, tall tree makes for confusing classes.
  • If you have virtual functions in your class, use the virtual constructor (if not, this is probably a bug)
  • With any hierarchy, the derivative and base classes Between A 'A-A' for Relationship
  • You should be aware that a virtual function can not be called at all ... then do not add expectations of mistake.
  • The rationale for the virtual function slows down is logic. It is dynamically bound, so this is often the case, whether it is mentioned in most cases, it is definitely the issue of debate. Profile and optimize instead :)
  • In C ++, do not use virtual when it is not needed. Meaning means marking the function virtual means - do not misuse it; the reader knows that "yes, it can be overridden!".
  • Prioritize the pure virtual interface in the hierarchy that mixes the implementation. It's clean and easy to understand.

The reality of the situation is that virtual functions are incredibly useful, and these colors of doubt are unlikely to come from balanced sources - the widely used virtual function Has been for a very long time. Other new languages ​​are otherwise adopting them as default.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -