C ++:隐藏规则背后的原理 [英] C++: rationale behind hiding rule
问题描述
在C ++中隐藏规则的理由是什么?
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); }
但这是不可能的。
我不认为它简化了编译器的工作,因为编译器必须能够取消隐藏函数,当你明确使用
使用
: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?
哼,所有三个答案似乎都很好,并显示隐藏规则的不同理由。我不知道应该接受哪个答案。
Hum, all the three answers seem to be good, and show different rationales for the hiding rule. I'm not sure about which answer I should accept.
推荐答案
这是一个毛茸茸的问题,但显然的想法是,功能有助于避免在更改基类时发生微妙的错误(否则可能会偷取之前被派生类处理的调用)。基类的变化可以影响派生类的编译结果,所以我不认为我理解100%的解释。
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.
我同意这个主题是所以经常讨论,可能隐藏实际上增加了C ++程序员的惊喜的数量。
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...
这篇关于C ++:隐藏规则背后的原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!