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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:46:33+00:00 2026-06-09T09:46:33+00:00

If i call an inherited method on a derived class instance does the code

  • 0

If i call an inherited method on a derived class instance does the code require the use of a vtable? Or can the method calls be ‘static’ (Not sure if that is the correct usage of the word)

For example:

Derived derived_instance;
derived_instance.virtual_method_from_base_class();

I am using msvc, but i guess that most major compilers implement this roughly the same way.

I am (now) aware that the behavior is implementation-specific, i’m curious about the implementation.

EDIT:

I should probaby add that the reason that we are interested is that the function is called a lot of times, and it is very simple, and i am not allowed to edit the function itself in any way, i was just wondering if would be possible, and if there would be any benifit to eliminating the dynamic-dispach anyway.

I have profiled and counted functions etc etc before you all get on my backs about optomization.

  • 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-09T09:46:34+00:00Added an answer on June 9, 2026 at 9:46 am

    Both of your examples will require that Derived has a constructor accepting a Base and create a new instance of Derived. Assuming that you have such a constructor and that this is what you want, then the compiler would “probably” be able to determine the dynamic object type statically and avoid the virtual call (if it decides to make such optimizations).

    Note that the behavior is not undefined, it’s just implementation-specific. There’s a huge difference between the two.

    If you want to avoid creating a new instance (or, more likely, that’s not what you want) then you could use a reference cast static_cast<Derived&>(base_instance).virtual_method_from_base_class(); but while that avoids creating a new object it won’t allow you to avoid the virtual call.

    If you really want to cast at compile time what you’re looking for is most likely the CRTP http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern which allows you to type everything at compile time, avoiding virtual calls.

    EDIT for updated question: In the case you’ve shown now, I would suspect many compilers capable of statically determining the dynamic type and avoiding the virtual call.

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

Sidebar

Related Questions

From my understanding, the virtual keyword allows you to use the base class' method,
I am trying to call a inherited method that must access private attributes from
I've inherited some code that uses the sqlsrv_connect method to instantiate a connection to
I would like a method in a base class to call another method in
I have the following helper method in a ViewModelBase class, which is inherited by
Is there a way of putting a static method in an abstract class that
How to specify overridden method of which base class to call in Delphi? Let
i'm wondering when an instance of a TInterfacedObject derived class is destroyed and who
Given a base class that is inherited by plethora of derived classes, and a
I'm overriding a virtual method, and I want to call inherited . But I

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.