-5

I m learning inheritance using C++.If I have a class A , and I have inherited class B and C from A, further I have derived class D from B and C (classical diamond problem). If I have a virtual function print() in A , I overloaded it in B and C, but not in D. Now, if I have an object of class D and I call print(), it would be an error. Is there any useful way to avoid this error?

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
aqsa
  • 15
  • 4
  • 3
    Please [show](https://stackoverflow.com/posts/48489540/edit) the relevant code instead of describing it. Diamond problem and multiple inheritance are things often rehashed by academia. Industry deals with other kind of problems. – Ron Jan 28 '18 at 17:52
  • Simple solution to the so called _diamond problem_ is: don't use multiple inheritance. – Ron Jan 28 '18 at 17:59
  • 1
    someone just please tell our teachers:-p – aqsa Jan 28 '18 at 18:03
  • Strongly related:[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). Helpful reading: [Inheritance — Multiple and Virtual Inheritance](https://isocpp.org/wiki/faq/multiple-inheritance) and [Prefer composition over inheritance?](https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – user4581301 Jan 28 '18 at 18:20

1 Answers1

0

You override the method in D, even if it is just to forward to one (or both) of the B and C overrides. (You have to do this even if you never call print on an expression whose static type is D, since an A* elsewhere could point to a D.)

Davis Herring
  • 36,443
  • 4
  • 48
  • 76