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 8901613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:24:09+00:00 2026-06-15T01:24:09+00:00

I have two classes foo and bar where one is a superclass of the

  • 0

I have two classes foo and bar where one is a superclass of the other, and they both have a method hello_world.

class foo {
    virtual void hello_world();
};
class bar : public foo {
    void hello_world();
};

My question is: is there any performance difference if I make it virtual
for bar‘s hello_world? It will become this:

class foo {
    virtual void hello_world();
};
class bar : public foo {
    virtual void hello_world();
};

I will mainly call hello_world from bar.
I know virtual function will make functions slow
because we do run time look up. But for this case, is there any difference?

  • 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-06-15T01:24:10+00:00Added an answer on June 15, 2026 at 1:24 am

    Calling a virtual function in a context where the class may, indeed, be polymorphic always has some performance impact compared to a function with the same logic that can be inlined. The primary reasons are that there is a small overhead calling a function, a small overhead in looking up which function needs to be called, and a major loss of optimization opportunities. The last point is typically the most expensive one.

    The original example didn’t compile, i.e., it is pretty clear that no performance analysis was done, yet. Worry about the performance impact once you have measured that your code runs too slow and that the specific call is, indeed, in the area where the performance problem comes from.

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

Sidebar

Related Questions

I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, one that inherits from the other. The base class is
I have two classes: public class Foo { public int FooId {get;set;} public virtual
I have two classes which I need to reference each other. class Foo {
Say I have two independent classes: class Foo { int bar; } class Baz
I have two classes that depend on each other: class Foo; //forward declaration template
Say I have two classes One, the Foo class, interfaces with the user, and
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes Foo and Bar . The tables in the database look
Given two model classes, Foo and Bar , I want Foo to have 3

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.