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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:06:20+00:00 2026-05-24T19:06:20+00:00

struct B { virtual void bar () {} virtual void foo () { bar();

  • 0
struct B
{
  virtual void bar () {}
  virtual void foo () { bar(); }
};

struct D : B
{
  virtual void bar () {}
  virtual void foo () {}
};

Now we call foo() using an object of B as,

B obj;
obj.foo();  // calls B::bar()

Question:
Should bar() will be resolved through virtual dispatch or it will be resolved using the static type of the object (i.e. B).

  • 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-24T19:06:21+00:00Added an answer on May 24, 2026 at 7:06 pm

    Entirely up to the implementation. Nominally it’s a virtual call, but you’re not entitled to assume that the emitted code will actually perform an indirection through a vtable or similar.

    If foo() is called on some arbitrary B*, then of course the code emitted for foo() needs to make a virtual call to bar(), since the referand might belong to a derived class.

    This isn’t an arbitrary B*, this is an object of dynamic type B. The result of a virtual or non-virtual call is exactly the same, so the compiler can do what it likes (“as-if” rule), and a conforming program can’t tell the difference.

    Specifically in this case, if the call to foo is inlined, then I’d have thought that the optimizer has every chance of de-virtualizing the call to bar inside it, since it knows exactly what’s in the vtable (or equivalent) of obj. If the call isn’t inlined, then it’s going to use the “vanilla” code of foo(), which of course will need to do some kind of indirection since it’s the same code used when the call is made on an arbitrary B*.

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

Sidebar

Related Questions

struct A { virtual void foo(); // unused and unimplemented virtual void bar ()
I've got the following structure: struct A { A(); virtual ~A(); virtual void Foo()
struct level0 { virtual void foo() = 0; }; struct level1 : level0 {
Consider: #include <iostream> using namespace std; struct A { virtual void f() { cout
Consider the following snippet: struct Base { virtual ~Base() {} virtual void Foo() const
This is what I would like to do using templates: struct op1 { virtual
Consider: struct device{ virtual void switchon() = 0 {} }; int main() { }
Code: struct IRenderingEngine { virtual void Initialize(int width, int height) = 0; virtual void
Consider the following snippet: struct ObjectInterface { virtual ~ObjectInterface() {} virtual void Print(std::ostream& target)
I need to call a const function from a non-const object. See example struct

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.