-2

Ok, this might be silly question but I can't figure out how to fix my problem.

Let's assume we have 4 classes

  1. class A is a Base class
  2. class B is derived from A with new methods (no override)
  3. class C is derived from A
  4. class D is derived from B (and also from A for inheritance)

my question is: how do I use a method defined in B in D? If D inherit from B I get "error: member 'xxx' found in multiple base classes of different types" if D does not inherit from B I get "use of undeclared identifier"

  • 5
    Please post a [mcve]. – R Sahu Jun 02 '18 at 17:09
  • Possible duplicate of [How to call a parent class function from derived class function?](https://stackoverflow.com/q/357307/608639), [Accessing base class's method with derived class's object which has a method of same name](https://stackoverflow.com/q/2437586/608639), [Call base class method from derived class object](https://stackoverflow.com/q/15853031/608639), etc. – jww Jun 02 '18 at 19:58
  • 1
    @jww: The problem with marking questions as duplicates is that you need to know exactly what OP meant, and if the question is a bit unclear, you're at best making a guess... – einpoklum Jun 02 '18 at 22:28

1 Answers1

2

Here's how it's done - based on your description:

class A {
protected:
    void foo();
};

class B : public A {
protected:
    void bar();
};

class D : public B {
protected:
    void baz() { B::bar(); }
};

Note that you should not have D inherit from A directly, except in very specific and rare cases. Inheritance is transitive.

Also, next time, Please post a Minimal, Complete, and Verifiable example and don't make us guess what you mean exactly.

In those cases in which the same method is available from the same subclasses from multiple inheritance paths are the "Diamond Pattern", and you can read about it here.

Gulzar
  • 23,452
  • 27
  • 113
  • 201
einpoklum
  • 118,144
  • 57
  • 340
  • 684