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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:54:44+00:00 2026-06-01T04:54:44+00:00

How is the decision taken internally (during runtime) as to which function to call,

  • 0

How is the decision taken internally (during runtime) as to which function to call, either the base class or derived class function. In the below sample code how is decision taken to call fn() of B and fn2() of A.

Class A { 
    virtual void fn() { std::cout << "A" < <std::endl; }
    virtual void fn2() { std::cout << "A-fn2" < <std::endl; }
    };

Class B : A
{
    void fn() { std::cout << "B" < <std::endl; }
}

int main() {
    B b = new B;
    A *a = &b;
    a->fn();
    a->fn2();
}

The output would be B and A-fn2()

Basically how is the decision taken at run time whether to call derived class function or base class function?

  • 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-01T04:54:46+00:00Added an answer on June 1, 2026 at 4:54 am

    What you have is known as Dynamic/Run-time polymorphism.
    The rule is:
    The method to call at run-time is selected at run-time, depending on the actual object pointed to by the pointer.
    How the compiler does this is completely implementation dependent. Typically, your code should only rely on the behaviors and not the internals of this. However, all known compilers implement this mechansim through a virtual table(vtbl) & virtual pointer(vptr) mechanism.

    How is run-time polymorphism implemented?

    Once a class has an virtual method that class is known as a polymorphic class and the compiler creates a vtbl for that class. The vtbl stores the addresses of all virtual methods in that class. The compiler also adds a special pointer vptr to every object of that class. The vptr points(stores the address of) to the vtbl.
    Once a class derives from such a polymorphic class, the compiler replaces the addresses of methods in the vtbl of the derived class with the address of overidding functions in the derived class.For non overidden methods the addresses are still that of the Base class methods.

    Thus, Each class has typically one vtbl, while each object instance has a vptr, which points to the vtbl. The vtbl of each class stores address of its own virtual methods.

    At run-time, the vptr inside the this pointer is fetched and further the appropriate method address in the vtbl is fetched & then it is called, this mechanism is called dynamic dispatch.
    Thus, due this mechanism the appropriate method to be called can be determined depending on type of the object which the pointer points to.

    This C++-Faq is a good further read:
    Inheritance — virtual functions

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

Sidebar

Related Questions

I have to make a decision of which database server to use for my
I had just taken the decision to change as many variables from unsigned to
I am in dilema to make decision on the below scenarios. Kindly need experts
I'm having a hard time making a design decision I have a class in
I'm trying to understand the design decision taken when the .NET framework creators designed
I have been trying to make a decision which approach to take to build
I have recently taken the decision of changing a vector of pointers into a
Which one of the following two pieces of code should be preferred over other?
What is the design decision to lean towards not returning an anonymous types from
Is the decision by Google similar to the one for SortedMultiSet (stackoverflow question) or

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.