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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:12:38+00:00 2026-06-01T19:12:38+00:00

while trying to analyse in greater depth inheritance mechanism of C++ I stumbled upon

  • 0

while trying to analyse in greater depth inheritance mechanism of C++ I stumbled upon the following example:

#include<iostream>

using namespace std;

class Base {
public:
    virtual void f(){
    cout << "Base.f" << endl; 
    }
};

class Left : public Base { //NOT VIRTUAL!!!
public:
void g(){ 
        f();
    }     
};

class Right : public Base{
public:
    virtual void f(){
    cout << "Right.f" << endl; 
    }
};

class Bottom : public Left, public Right{
public:
    Bottom(int arg){ }
    //void f() { }
};

int main(int argc,char **argv)
{
    Bottom* b = new Bottom(23);
    b->g();
}

It is clear that calling

b->f()

is ambiguous, so there is no unique method f() on object Bottom. Now, calling

b->g()

works fine and prints

Base.f

Well, as far as I see this:

  1. static type is Bottom, so we call its g() method, as it is non-virtual
  2. g() method is inherited from Left, so we call this inherited method
  3. Now, g() in Left tries to call virtual method f(). According to C++ sepcification, we call f() method of a dynamic type of our pointer (which is Bottom)

BUT Bottom does not have method f()… at least not a unique one. Why does this program executes Left::Base::f() rather than Right::Base::f() or why does it simply not states that call to f() is ambiguous from Bottom?

  • 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-01T19:12:40+00:00Added an answer on June 1, 2026 at 7:12 pm

    The short answer is that (as you noted), Bottom does not have method f(), so there is no need in trying to call it.

    Bottom contains the two subobjects Left and Right. Each of them inherits from Base, so Bottom contains the member functions Left::f() and Right::f(), but no Bottom::f(). Since Bottom does not override Left::f() (using Right::f() for example), Base::f() is the unique final overrider in Left::g().

    cf. the example in 10.3.9 from the C++03 standard.

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

Sidebar

Related Questions

while trying to analyse in greater depth inheritance mechanism of C++ I stumbled upon
While trying to implement an MVC file upload example on Scott Hanselman's blog. I
So, I'm trying to analyse some of my program's MySQL queries. However, while I've
While trying to open an excel using ApachePOI I get org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the
While trying to optimize some code I came across another question.. Using usergroups and
While trying to compile the following code, which is enhanced version of read build
I'm trying to analyse some code here that's been designed using an MVP Approach.
While trying to build the mysql2 gem with ruby 1.9.2-p320 on Fedora 16, I
While trying to deploy an ASP.NET MVC3 locally on my IIS7 , I'm facing
While trying to search with id sorting (and paginating) im getting from the log:

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.