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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:05:11+00:00 2026-06-13T05:05:11+00:00

Coming from a java background I have never encountered the diamond problem where multiple

  • 0

Coming from a java background I have never encountered the diamond problem where multiple inheritance can cause overriding problems as described in detail at http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem

But if this problem occurs will the c++ runtime complain or is it random which implementation of the super class method will be invoked ?

I’ve read the ‘Mitigation’ part of article but did’nt fully understand it.

  • 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-13T05:05:12+00:00Added an answer on June 13, 2026 at 5:05 am

    The compiler will catch a program encountering the diamond problem by diagnosing any ambiguity.

    One solution is to remove the ambiguity. This can be done when referring to members by using explicit name qualification:

    struct B {
        int bar;
    };
    
    struct D1 : B {};
    struct D2 : B {};
    
    struct E : D1, D2 {};
    
    int main() {
        E e;
        e.D1::bar = 1; // explicitly set D1::bar, not D2::bar.
    }
    

    Or if you want to access a base sub-object as in: B *b = new E; It’s ambiguous whether you want the base sub-object from D1 or D2. Using an explicit cast to one of those intermediate types resolves the ambiguity.

    B *b = static_cast<D2*>(new E);
    

    Also note that down-casting from a B* to an E* is not possible to do statically; the compiler doesn’t know which B is being pointed to so it doesn’t statically know how to adjust the pointer to get back to E. This is where dynamic_cast becomes necessary.

    E *e = new E;
    B *b1 = static_cast<D1*>(e);
    B *b2 = static_cast<D2*>(e);
    assert(b1 != b2);
    assert(dynamic_cast<E*>(b1) == dynamic_cast<E*>(b2));
    assert(e == dynamic_cast<E*>(b1));
    

    Another solution is to sidestep the issue with virtual inheritance, thus avoiding multiple base sub-objects of the same type.

    struct B {
        virtual void foo() = 0;
        virtual ~B() = default;
        int bar;
    };
    
    struct D1 : virtual B {};
    struct D2 : virtual B {};
    
    struct E : D1, D2 {
        virtual void foo() override {
            bar = 1; // no ambiguity because there's only a single B base sub-object
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple question. Coming from a java background and having worked extensively
I'm coming from a Java background, with its class-based inheritance model, trying to get
I'm relatively new to services in java (coming from a C# background) and have
Coming from a Java background, I want a class to have certain attributes and
I have been playing around with node.js, and coming from a Java background, I
Coming from a Java background, I'm wondering why List in Scala doesn't have a
I have just 'inherited' a Java-project and not coming from a Java-background I am
Coming from a C++ background I have to master the complexity of the Java
Background: I'm coming from Java background so not too familiar with Javascript. We are
Coming from a Java background, this is the way I'm thinking: The server provides

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.