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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:04:42+00:00 2026-06-17T14:04:42+00:00

I would like some help explaining this phenomenom: #include <iostream> using namespace std; class

  • 0

I would like some help explaining this phenomenom:

#include <iostream>
using namespace std;

class A
{
public:
    void m() {cout<<"A::m "<<this<<endl;};
};

class B1:  public A
{
public:
    void m() {cout<<"B::m "<<this<<endl;};
};

class B2:  public A ,public B1
{
};

class D : public B2
{};

int main()
{
    B2 b;
    D d;
    A* a = &b; // Row 27
    //error: a = &d;  Base class 'A' is ambiguous // Row 28
    return 0;
}

Why does the code in Row27 work but the code in Row28 doesnt?
Thank in advance!

Note: I am well aware of virtual inheritance, I just want to know what is the difference between Row27 and Row28 – why one throw a compilation error when the other not?

  • 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-17T14:04:44+00:00Added an answer on June 17, 2026 at 2:04 pm

    Because you are using non-virtual inheritance to derive from A, which indirectly creates two sub-objects of type A in every object of type D. The compiler cannot disambiguate which sub-object you refer to when doing the pointer-to-derived to pointer-to-base conversion, and issues an error.

    In order to have only one sub-object of type A generated for objects of type D, you have to make inheritance from A virtual along the inheritance paths which make D derive from A:

    class B1:  virtual public A
    {
        // ...
    };
    
    class B2:  virtual public A, public B1
    {
    };
    

    EDIT:

    I tried to compile your example on Visual Studio 2010 SP1, which gives me a warning about the definition of B2:

    class B2: public A, public B1
    {
    };
    
    1>sotest.cpp(18): warning C4584: 'B2' : base-class 'A' is already a base-class of 'B1'
    1>          sotest.cpp(6) : see declaration of 'A'
    1>          sotest.cpp(11) : see declaration of 'B1'
    

    In other words, for some reason VC10 seems to consider the inheritance from A redundant and ignores it. This is why the assignment A* a = &b; compiles: class B2 actually inherits from A only once (through B1). The same is not true of D, because VC10 has probably no redundant inheritance to ignore, and D effectively inherits from A twice (through B1 and through B2).

    I ignore the reasons why VC10 behaves this way, and I do not know if there is a compiler option to suppress this behavior. Remarkably, both GCC 4.7.2 and Clang 3.2 refuse to compile the assignment A* a = &b;.

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

Sidebar

Related Questions

I need some help figuring out how I should do this. I would like
I would like some help in continuing my code using openCV library in order
i would like some help with the following. this is to do with Asynchronous
I new to using ASP.net-mvc and would like some help. I created a web
I am using Mono Develop For Android and would like some help with using
I would like some help in getting this regex to accept the space character.
I've encountered this interview question and would like some help in trying to understand
This may be a simple question but i would like some help to find
I would like some help about an Android Application. I used the new Google
I would like some help parsing a dynamic (folder.subfolders) xml with xslt, I tried

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.