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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:59:05+00:00 2026-05-23T13:59:05+00:00

I’ve searched for questions, looking at forums, books, etc. I can recognize a polymorphic

  • 0

I’ve searched for questions, looking at forums, books, etc. I can recognize a polymorphic behavior of methods, and there are lots of simple examples when an invoked method is decided in compile or runtime. But I was confused by this code, where a class C inherits from B that inherits from A:

class A {
protected:
    int x;

public: 
    virtual void change() = 0;
    virtual void change(int a) { x = a; }
};

class B : public A {
public: 
    void change() { x = 1; }
};

class C : public B {
public: 
    void change() { x = 2; }
    void change(int a) { x = a*2; }
};

int main () { 
    B *objb = new B();
    C *objc = new C();
    A *obja;
    objb->change();
    obja = objc;
    objc->change();
    obja->change(5);
    // ...
}

Many examples tells (and it is clear) that a polymorphic behavior occurs and it is decided in runtime what method to call when the following line is executed:

obja->change(5);

But my questions are:

  1. What happens when I call the following (overrided from a pure virtual)?

    objb->change();
    
  2. What happens when I call the following (overrided from a virtual, but non-pure)?

    objc->change(5);
    

Since the class declaration of the pointer variables are the same of the objects, should the method calling be decided at compile or runtime?

  • 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-23T13:59:05+00:00Added an answer on May 23, 2026 at 1:59 pm
    objb->change()
    

    calls B::change() because objb contains address of an object of type B

    objc->change(5);
    

    calls C::change(int) because objc contains address of an object of type C

    The method calling will be still dynamic/run time because the methods B::change() & C::change(int) are still virtual, because the virtual attribute in inherited.

    To answer the question of whether the functions are dynamically dispatched or at compile time

    The answer is No can definitely say whether it will be a compile time dispatch or dynamic dispatch. The dynamic/run time dispatch takes place in the first place because the compiler cannot definitely decide which versions of the functions to call at compile time, So if a compiler can deduce a definite manner as to which function to call, the dispatch might very well be decided at compile time itself.

    Having said so, whether the dispatch happens at run time or compile time does not change the semantics of calling which version of overidden function gets finally called because the C++ standard explicitly states the rules of which functions of functions in this regard.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.