Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 845821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:31:00+00:00 2026-05-15T06:31:00+00:00

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

  • 0

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?

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


One reason I can think of is virtual function may be slower than normal functions due to v-table lookup.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:31:00+00:00Added an answer on May 15, 2026 at 6:31 am

    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.

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

    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.

    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:

    • 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.