Search code examples
c++language-design

C++: rationale behind hiding rule


What's the rationale behind the hiding rule in C++?

class A { void f(int); }
class B : public A { void f(double); } // B::f(int) is hidden
  • If it is a meaningful feature I think it should also be possible to hide functions without defining new functions with the same name: something like this:

     class B : public A { hide void f(double); }
    

but this is not possible.

  • I don't think it simplifies compilers job, since compilers must anyway be able to unhide functions when you explicitly use the using directive:

     class B : public A { using A::f; void f(double); } // B::f(int) NOT hidden
    

So, how come there is a hiding rule?


Solution

  • It's an hairy question, but apparently the idea is that this hiding feature helps avoiding subtle bugs when making changes to a base class (that could otherwise "steal" calls that before would have been handled by the derived class). Still a change in a base class can influence the result of compilation of derived classes so I don't think I understand 100% this explanation.

    I agree that this topic is so frequently discussed that probably the hiding actually increases the amount of "surprises" in C++ programmers.

    A detailed discussion about this issue can be found here...