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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:01:18+00:00 2026-05-23T09:01:18+00:00

Why is it that in the code below the compiler complains that PureAbstractBase is

  • 0

Why is it that in the code below the compiler complains that PureAbstractBase is an ambiguous base class of MultiplyInheritedClass? I realize I have two copies of the PureAbstractBase in MultiplyInheritedClass and that FirstConreteClass and SecondConreteClass should be derived virtually because they’re the middle row of the diamond (and that does indeed fix the problem with the code below). But even though I have two copies of the interface why is it that the code in MultiplyInheritedClass does not just override both and unambiguously pick the interface class defined in MultiplyInheritedClass?

#include <iostream>
using namespace std;

class PureAbstractBase {
  public:
    virtual void interface() = 0;
};

// I know that changing the following line to:
// class FirstConcreteClass : public virtual PureAbstractBase {
// fixes the problem with this hierarchy
class FirstConcreteClass : public PureAbstractBase {
  public:
    virtual void interface() { implementation(); }
  private:
    void implementation() { cout << "This is object FirstConcreteClass\n"; }
};

// I know that changing the following line to:
// class SecondConcreteClass : public virtual PureAbstractBase {
// fixes the problem with this hierarchy
class SecondConcreteClass : public PureAbstractBase {
  public:
    virtual void interface() { implementation(); }
  private:
    void implementation() { cout << "This is object SecondConcreteClass\n"; }
};

class MultiplyInheritedClass : public FirstConcreteClass,
                               public SecondConcreteClass {
  public:
    virtual void interface() { implementation(); }
  private:
    void implementation() { cout << "This is object MultiplyInheritedClass\n"; }
};

Further, why do I not have issues with the following hierarchy? Doesn’t the ConcreteHandler class have three copies of the AbstractTaggingInterface in this case? So why doesn’t it have the same issue as the example above?

#include <iostream>
using namespace std;

class AbstractTaggingInterface {
  public:
    virtual void taggingInterface() = 0;
};

class FirstAbstractHandler : public AbstractTaggingInterface {
  public:
    virtual void taggingInterface() { cout << "FirstAbstractHandler\n"; }
    virtual void handleFirst() = 0;
};

class SecondAbstractHandler : public AbstractTaggingInterface {
  public:
    virtual void taggingInterface() { cout << "SecondAbstractHandler\n"; }
    virtual void handleSecond() = 0;
};

class ThirdAbstractHandler : public AbstractTaggingInterface {
  public:
    virtual void taggingInterface() { cout << "ThridAbstractHandler\n"; }
    virtual void handleThird() = 0;
};

class ConcreteHandler : public FirstAbstractHandler,
                        public SecondAbstractHandler,
                        public ThirdAbstractHandler {
  public:
    virtual void taggingInterface() = { cout << "ConcreteHandler\n"; }
    virtual void handleFirst() {}
    virtual void handleSecond() {}
    virtual void handleThird() {}
};

I am trying to wrap my head around all of this because I had a conversation with a colleague recently where he claimed that if you were inheriting from pure virtual classes (interfaces) without any data members then virtual inheritance was not necessary. I think understanding why the former code example does not work and the latter does would go a long way to getting this straight in my head (and clear up what exactly he meant by his comment). Thanks in advance.

  • 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-23T09:01:19+00:00Added an answer on May 23, 2026 at 9:01 am

    You need virtual inheritance to overcome the diamond-ambiguity:

    class FirstConcreteClass  : public virtual PureAbstractBase { ... };
    class SecondConcreteClass : public virtual PureAbstractBase { ... };
    

    Long-winded explanation: Suppose you have this:

    // *** Example with errrors! *** //
    struct A { virtual int foo(); };
    struct B1 : public A { virtual int foo(); };
    struct B2 : public A { virtual int foo(); };
    struct C: public B1, public B2 { /* ... */ };  // ambiguous base class A!
    
    int main() {
      A * px = new C;                              // error, ambiguous base!
      px->foo();                                   // error, ambiguous override!
    }
    

    The inheritance of the virtual function foo is ambiguous because it comes in three ways: from B1, from B2 and from A. The inheritance diagram forms a “diamond”:

       /-> B1 >-\
    A->          ->C
       \-> B2 >-/
    

    By making the inheritance virtual, struct B1 : public virtual A; etc., you allow any baseclass of C* to call the correct member:

    struct A { virtual int foo(); };
    struct B1 : public virtual A { virtual int foo(); };
    struct B2 : public virtual A { virtual int foo(); };
    struct C: public B1, public B2 { virtual int foo(); };
    

    We must also define C::foo() for this to make sense, as otherwise C would not have a well-defined member foo.

    Some more details: Suppose we now have a properly virtually-inheriting class C as above. We can access all the various virtual members as desired:

    int main() {
      A * pa = new C;
      pa->foo();      // the most derived one
      pa->A::foo();   // the original A's foo
    
      B1 * pb1 = new C;
      pb1->foo();     // the most derived one
      pb1->A::foo();  // A's foo
      pb1->B1::foo(); // B1's foo
    
      C * pc = new C;
      pc->foo();      // the most derived one
      pc->A::foo();   // A's foo
      pc->B1::foo();  // B1's foo
      pc->B2::foo();  // B2's foo
      pc->C::foo();   // C's foo, same as "pc->foo()"
    }
    

     

    Update: As David says in the comment, the important point here is that the intermediate classes B1 and B2 inherit virtually so that further classes (in this case C) can inherit from them while simultaneously keeping the inheritance from A unambiguous. Sorry for the initial mistake and thanks for the correction!

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

Sidebar

Related Questions

I have this javascript code below that uses jquery, it is suppoed to be
I have the code below that hides and shows the navigational bar. It is
Background: I have a WPF UserControl (MainControl - not shown in code below) that
I have written a very simple WCF service, that worked fine (code below), then
The code below works great. I have a MySQL database that contains book titles
I have a problem with the code below. Compiler says incompatible types, java.lang.object[][] required
Given the code below, the compiler is showing a message pointing that error: templates
I understand that the code below would result segmentation fault because at the cstr
The code below shows a sample that I've used recently to explain the different
The code below shows me (I think) that the for each loop is about

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.