Preventing overriding and/or hiding base class function (C++ 11)

后端 未结 1 1323
遥遥无期
遥遥无期 2021-01-17 17:20

I\'ve wanted to prevent even the hiding of base class non-virtual function for a while since I learned C++, and I\'m not sure if this would be ethical, but C++ 11 features g

相关标签:
1条回答
  • 2021-01-17 17:35

    I'd say that, yes, it's bad practice. You introduced polymorphism when you didn't want it, just to prevent name hiding.

    But what's so bad about name hiding? Obviously, for whatever reason, the design of Derived desires to hide the function foo in its base class; that is what it does — perhaps it makes its own call to Base::foo then performs some additional activity that is required within Derived.

    Trying to subvert that is bad enough; trying to do it by co-opting language features that you don't want is even worse.

    If you really need to call the base function, use derived.Base::foo(), but be aware that you are essentially breaking the API of the class Derived, which you should instead use as documented.

    0 讨论(0)
提交回复
热议问题