Search code examples
c++multiple-inheritanceoverriding

What to do when a class doesn’t need to override a method inherited from multiple bases?


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?


Solution

  • 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.)