lurscher lurscher - 1 month ago 14
C++ Question

a way in c++ to hide a specific function

i have an inheritance

struct A : public B
, i want to hide individual functions from B, is this possible?

i know the opposite is possible using
using BMethod
in the A declaration.

cheers

Answer

There is an issue here: this would be a direct violation of the Liskov Substitution Principle, namely A would not act as a B any longer.

If you wish to reuse B implementation, the solution is simply to do so:

class A
{
public:
  void foo() { return b.foo(); }
  void bar() { return b.bar(); }
  // ...

private:
  B b;
};

Don't abuse inheritance, use composition instead