Pregunta

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.

¿Fue útil?

Solución 2

This should work:

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

Otros consejos

There are a few problems, first val is private and so that needs to be made protected. Next you have a diamond of death so you need to virtual public for One and Two. You also are trying to call Add using a type but you need an instance of each class:

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:virtual public Number{
public:
     One(){
          val=1;
    }
};

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

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

It could be argued that using protected data is bad but it depends on your case but to be complete it is also a choice to keep val a private data member and use a protected constructor and that would look like this:

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

class One: virtual public Number{
public:
     One() : Number( 1 ) {

     }
};

class Two: virtual public Number{
public:
     Two() : Number(2) {
     }
};  

You need to create objects of the respective types.

class Three : public One, public Two
{
public:
     Three()
     {
          Add(One());
          Add(Two());
     }
};

But I don't see why you need MI here. Inheriting from Number would be sufficient.

The problem is not just the syntax. But what exactly are you trying to do?

You can. But where is the instance of One declared? you have to declare it first. Remember Two::Add(One); is not a declaration rather a call statement.

What you are doing is equivalent to let's say

process_number(One);

Where process number is a function.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top