0

I have a Class B, which is inheriting Class A. I have a Class C, which is inheriting Class B and Class A;

There is a variable named "a" in class A which I have to access in class D, but not by using resolution operator on class B for example , d.B::a;

#include<bits/stdc++.h>
using namespace std;

class A{
    public:
        int a;
};

class B : public A{
    public:
        int b;
};

class D : public B, public A{
    public:
        int d;
};

int main() {
    D d;
    d.B::a = 1; // This is working correctly on this path class D -\> class B -\> class A
    d.a = 2;
/*
But This line gives the following error
Non-static member 'a' found in multiple base-class subobjects of type 'A':
class D -> class B -> class A
class D -> class A
*/
    return 0;

}

int main() {
D d;
d.D::a = 2; //This is not working correctly on this path class D -\> class A
d.A::a = 2; //This is not working correctly on this path class D -\> class A
//How do I use Scope Resolution to access variable "a" from second path ?!
return 0;
}
Ravi
  • 1
  • 1
  • 2
    Does this answer your question? [How can I avoid the Diamond of Death when using multiple inheritance?](https://stackoverflow.com/questions/137282/how-can-i-avoid-the-diamond-of-death-when-using-multiple-inheritance) – Andy Oct 26 '22 at 12:31
  • 1
    Of course you get an error: A member with name `a` exists twice, so the compiler doesn't know which one you mean. Be aware that `D` once inherits `A` *directly* but then yet another time *indirectly* via `B`. Usually these things occur in a diamond pattern (both `B` and `C` inheriting from `A` and `D` inheriting from both `B` and `C`) – I don't see any value in this strange triangle arrangement. Why would `D` need yet another `A` when it gets one already from `B`??? – Aconcagua Oct 26 '22 at 12:34
  • See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). – prapin Oct 26 '22 at 18:27

2 Answers2

0

B already inherits from A, so D should only inherit from B:

  class A{
       public:
           int a;
  };

  class B : public A{
        public:
            int b;
  };

  class D : public B{
        public:
            int d;
  };

If you had another class C that also inherits from A, and you would want D to inherit from B and C, then you would have the diamond problem. Then, to avoid ambiguity, you should write:

  class A{
       public:
           int a;
  };

  class B : virtual public A{
        public:
            int b;
  };

  class C : virtual public A{
        public:
            int b;
  };

  class D : public B, public C{
        public:
            int d;
  };
Yhibo
  • 1
  • 1
0

Virtual inheritance is key words for your problem.

Try this one :

#include<bits/stdc++.h>
using namespace std;

class A{
    public:
        int a;
};

class B : virtual public A{
    public:
        int b;
};

class D : public B, virtual public A{
    public:
        int d;
};

int main() {
    D d;
    d.B::a = 1; // This is working correctly on this path class D -\> class B -\> class A
    d.a = 2;

    std::cout << d.a;
    
/*
But This line gives the following error
Non-static member 'a' found in multiple base-class subobjects of type 'A':
class D -> class B -> class A
class D -> class A
*/
    return 0;

} 
embeddedstack
  • 362
  • 1
  • 13