-1

I have something like this sample of code:

    class A
    {
       public:
         bool ComputeSomething(double &x, double y);
       ...
    };

    class B: public A{...}
    class C: public A{...}
    class D: public B, public C
    {
      bool FindSomething(const A& a);
      ...
    }

My problem is that in class D the function FindSomethng does not know if A comes from B or C. Is there a way to block A in C so that the methods in C from A to be used just in C and in D to use the methods of A from B? I know this is kind od a stupid question. I need to make this to work just by modifying class C because class B and D are to developed to support such changings...

king_nak
  • 11,313
  • 33
  • 58
student
  • 352
  • 2
  • 15
  • 1
    You might be looking for virtual inheritance. But it's hard to determine what you mean. What does this mean? `the function FindSomethng does not know if A comes from B or C` Do you get some error when compiling? What is it? – underscore_d Jun 30 '17 at 08:59
  • 3
    Possible duplicate of [C++ multiple inheritance with base classes deriving from the same class](https://stackoverflow.com/questions/25689708/c-multiple-inheritance-with-base-classes-deriving-from-the-same-class) – underscore_d Jun 30 '17 at 09:02
  • I get an error such that A is ambiguos after I introduced class C – student Jun 30 '17 at 09:02
  • _Where_ do you get the error? Nothing you have posted would result in ambiguity, but then you've ellipsised out so much of your code that it's of very little use at all. When posting code that generates an error, post the part that does. When talking about errors, quote them in full, including the line/column number and any other information they give. – underscore_d Jun 30 '17 at 09:09
  • This is the so called "Diamond problem". – Marco Luzzara Jun 30 '17 at 09:28

1 Answers1

1

I suggest that you read this FAQ on virtual/multiple inheritance.

In C++, if you have multiple inheritance, either it is non-virtual in which case you get multiple copies of common base classes, or it is virtual in which case there is just one copy of common base classes.

In most cases it is simply better to avoid multiple inheritance.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43