Search code examples
c++multiple-inheritancediamond-problem

Access Individual classes of a derived class, which is derived from two same base class


I have a base class called Number. Class One and Two are derived from Number. Now I define another class Three, where I need to access individual base classes from the multiple inheritance:


class Number{
  protected:
     int val;
  public:
    Number(){
        val=0;
    }
    void Add(Number n){//Receives another Number class instance and add the value
        val+=n.val;
    }
};

//class One derived from Number
class One:public Number{
  public:
     One(){
          cal=1;
     }
};

//class two derived from Number
class Two:public Number{
  public:
     Two(){
          val=2;
     }
};

class Three:public One,public Two{
  public:
     Three(){
          Two::Add(One);//--How can i pass instance of class One Here
     }
};

I tried One::Number and Two::Number, but no use.


Solution

  • This should work:

    Two::Add(*(One*)this);