1

I was going through the diamond problem and thought will work on various scenarios. And this is one among them which I was working on.

#include <iostream> 
using namespace std;
class MainBase{

    public:
    int mainbase;
    MainBase(int i):mainbase(i){}
    void geta()
    {
        cout<<"mainbase"<<mainbase<<endl;
    }
};
class Derived1: public MainBase{

    public:
    int derived1;
    int mainbase;
    Derived1(int i):MainBase(i),derived1(i) {mainbase = 1;}
    public:
    void getderived1()
    {
        cout<<"derived1"<<derived1<<endl;
    }

};
class Derived2: public MainBase{

    public:
    int derived2;
    int mainbase;
    Derived2(int i):MainBase(i),derived2(i){mainbase = 2;}
    public:
    void getderived2()
    {
        cout<<"derived2"<<derived2<<endl;
    }
};
class Diamond: public Derived1, public Derived2{

    public:
    int diamond;
    int mainbase;
    Diamond(int i,int j, int x):Derived1(j),Derived2(x),diamond(i){mainbase=3;}
    public:
    void getdiamond()
    {
        cout<<"diamond"<<diamond<<endl;
    }
};
int main()
{
    Diamond d(4,5,6);
//    cout<< d.MainBase::mainbase;
    cout<<"tested"<<endl;
    cout<<d.mainbase;
    cout<<d.Derived2::mainbase<<endl;
    cout<<d.Derived1::mainbase<<endl;
    /*cout<<d.Derived2::MainBase::mainbase<<endl;
    cout<<d.Derived1::MainBase::mainbase<<endl;*/
}

I am now wondering how to I access MainBase class mainbase variable? Any inputs.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Lakshmi
  • 1,759
  • 4
  • 18
  • 23
  • Fix the code, please. Select it all and hit 010101. – jkeys Aug 12 '09 at 05:12
  • Indeed, it's a nightmare to look at. :( – Michael Foukarakis Aug 12 '09 at 05:13
  • 1
    The diamond problem: "In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?" source: http://en.wikipedia.org/wiki/Diamond_problem – Adriaan Aug 12 '09 at 07:36

3 Answers3

5

You do what you have done there:

cout<<d.Derived2::MainBase::mainbase<<endl;
cout<<d.Derived1::MainBase::mainbase<<endl;

But, it might not do what you are trying to achieve. Possibly, you should be using virtual inheritance? What you have means there will be two copies of the MainBase members in your object, one for each inheritance track.

(From MSDN).

When a base class is specified as a virtual base, it can act as an indirect base more than once without duplication of its data members. A single copy of its data members is shared by all the base classes that use it as a virtual base.

Probably something like this will suit you better:

class Derived1: virtual public MainBase{
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
2

Your code won't compile, there is an ambiguity when referring to Mainbase as a base of a Diamond instance. You need to use virtual in the derived classes (Derived1, Derived2) to resolve this ambiguity by allowing them to share a single instance of base class, like this:

class Derived1: virtual public Mainbase {
/* do your thing here*/
};
Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
0

You should be able to access it like this from the Diamond class:

Diamond::foo() {
    Mainbase::mainbase = 0;
}

Assuming that your code compiles properly in the first place. See Michael Foukarakis' answer for more about compilation. Also, see this excellent article from DDJ: "Multiple Inheritance Considered Useful" about how to do this correctly.

Community
  • 1
  • 1
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319