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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:34:37+00:00 2026-05-10T19:34:37+00:00

I have an abstract base class which acts as an interface. I have two

  • 0

I have an abstract base class which acts as an interface.

I have two ‘sets’ of derived classes, which implement half of the abstract class. ( one ‘set’ defines the abstract virtual methods related to initialization, the other ‘set’ defines those related to the actual ‘work’. )

I then have derived classes which use multiple inheritance to construct fully defined classes ( and does not add anything itself ).

So: ( bad pseudocode )

class AbsBase {   virtual void init() = 0;   virtual void work() = 0; }  class AbsInit : public AbsBase {   void init() { do_this(); }   // work() still abs }  class AbsWork : public AbsBase {   void work() { do_this(); }   // init() still abs }  class NotAbsTotal : public AbsInit, public AbsWork {   // Nothing, both should be defined } 

First of all, can I do this? Can I inherit from two classes which are both derived from the same Base? (I hope so).

Here is the ‘real problem’, though (I lied a bit above to simplify the example).

What I have really gone and done is add non abstract accessors methods to the base class:

class AbsBase { public:   void init() { init_impl(); }   void work() { work_impl(); }  private:   virtual void init_impl() = 0;   virtual void work_impl() = 0; } 

Because, a common idiom is to make all virtual methods private.

Unfortunately, now both AbsInit, and AbsWork inherit these methods, and so NotAbsTotal inherits ‘two of each’ ( I realize I may be butchering what is really happening at compile time ).

Anyway, g++ complains that: ‘request for member init() is ambiguous’ when trying to use the class.

I assume that, had I used my AbsBase class as a pure interface, this would have been avoided ( assuming that the top example is valid ).

So: – Am I way off with my implementation? – Is this a limitation of the idiom of making virtual methods private? – How do I refactor my code to do what I want? ( Provide one common interface, but allow a way to swap out implementations for ‘sets’ of member functions )

Edit:

Seems I am not the first one: http://en.wikipedia.org/wiki/Diamond_problem

Seems Virtual Inheritance is the solution here. I have heard of virtual inheritance before, but I have not wrapped my head around it. I am still open to suggestions.

  • 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. 2026-05-10T19:34:37+00:00Added an answer on May 10, 2026 at 7:34 pm

    It looks like you want to do virtual inheritance. Whether that turns out to actually be a good idea is another question, but here’s how you do it:

     class AbsBase {...}; class AbsInit: public virtual AbsBase {...}; class AbsWork: public virtual AbsBase {...}; class NotAbsTotal: public AbsInit, public AbsWork {...}; 

    Basically, the default, non-virtual multiple inheritance will include a copy of each base class in the derived class, and includes all their methods. This is why you have two copies of AbsBase — and the reason your method use is ambiguous is both sets of methods are loaded, so C++ has no way to know which copy to access!

    Virtual inheritance condenses all references to a virtual base class into one datastructure. This should make the methods from the base class unambiguous again. However, note: if there is additional data in the two intermediate classes, there may be some small additional runtime overhead, to enable the code to find the shared virtual base class.

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

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 57k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Ok, I found one mechanism to do this - create… May 11, 2026 at 8:33 am
  • added an answer RFC 1738 which defines the URL specification states that only… May 11, 2026 at 8:33 am
  • added an answer A relative URL without a scheme (http: or https:) is… May 11, 2026 at 8:33 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.