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

  • SEARCH
  • Home
  • 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 6108333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:17:06+00:00 2026-05-23T14:17:06+00:00

I know that virtual functions have an overhead of dereferencing to call a method.

  • 0

I know that virtual functions have an overhead of dereferencing to call a method. But I guess with modern architectural speed it is almost negligible.

  1. Is there any particular reason why all functions in C++ are not virtual as in Java?
  2. From my knowledge, defining a function virtual in a base class is sufficient/necessary. Now when I write a parent class, I might not know which methods would get over-ridden. So does that mean that while writing a child class someone would have to edit the parent class. This sounds like inconvenient and sometimes not possible?

Update:
Summarizing from Jon Skeet’s answer below:

It’s a trade-off between explicitly making someone realize that they are inheriting functionality [which has potential risks in themselves [(check Jon’s response)] [and potential small performance gains] with a trade-off for less flexibility, more code changes, and a steeper learning curve.

Other reasons from different answers:

Virtual functions cannot be in-lined because inlining have to happen at runtime. This have performance impacts when you expect you functions benefits from inlining.

There might be potentially other reasons, and I would love to know and summarize them.

  • 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-23T14:17:07+00:00Added an answer on May 23, 2026 at 2:17 pm

    There are good reasons for controlling which methods are virtual beyond performance. While I don’t actually make most of my methods final in Java, I probably should… unless a method is designed to be overridden, it probably shouldn’t be virtual IMO.

    Designing for inheritance can be tricky – in particular it means you need to document far more about what might call it and what it might call. Imagine if you have two virtual methods, and one calls the other – that must be documented, otherwise someone could override the “called” method with an implementation which calls the “calling” method, unwittingly creating a stack overflow (or infinite loop if there’s tail call optimization). At that point you’ve then got less flexibility in your implementation – you can’t switch it round at a later date.

    Note that C# is a similar language to Java in various ways, but chose to make methods non-virtual by default. Some other people aren’t keen on this, but I certainly welcome it – and I’d actually prefer that classes were uninheritable by default too.

    Basically, it comes down to this advice from Josh Bloch: design for inheritance or prohibit it.

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

Sidebar

Related Questions

I recently came to know that in C++ pure virtual functions can optionally have
I know that it's OK for a pure virtual function to have an implementation.
I know that I can share files using Shared Folders in Virtual PC, but
I know that lot of questions about HTML sanitizers have appeared in SO, but
Pure virtual functions are those member functions that are virtual and have the pure-specifier
I know that compilers have much freedom in implementing std::type_info functions' behavior. I'm thinking
We all know what virtual functions are in C++, but how are they implemented
I know that all of these will be compiled together into one file but
I know that similar question was asked several times, but still I can't make
I've already read Are inline virtual functions really a non-sense? . But I still

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.