且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么允许通过基类的指针调用派生类的私有虚方法?

更新时间:2023-02-15 15:36:53

访问控制在编译时执行,而不是运行时。一般来说,调用 f()是为了知道 ptr $ c>指向的对象的运行时类型, ,所以没有检查派生类的访问说明符。这是允许调用的原因。

Access control is performed at compile time, not runtime. There's no way in general for the call to f() to know the runtime type of the object pointed to by ptr, so there's no check on the derived class's access specifiers. That's why the call is permitted.

至于为什么B类允许使用私有函数覆盖 - 我不确定。当然,B违反了它从A的继承隐含的接口,但一般来说,C ++语言并不总是强制执行接口的继承,所以事实上它是Just Plain Wrong并不意味着C ++会阻止你。

As for why class B is permitted to override using a private function at all - I'm not sure. Certainly B violates the interface implied by its inheritance from A, but in general the C++ language doesn't always enforce inheritance of interface, so the fact that it's Just Plain Wrong doesn't mean C++ will stop you.

所以我猜想这个类B可能有一些用例 - 替换仍然使用动态多态性,但是静态B不是A的替代(例如,可以有模板调用 f ,这将使用A作为参数,但不是B作为参数)。可能有些情况下,这正是你想要的。当然,这可能只是一些其他考虑的意想不到的后果。

So I'd guess that there's probably some use case for this class B - substitution still works with dynamic polymorphism, but statically B is not a substitute for A (e.g. there can be templates that call f, that would work with A as argument but not with B as argument). There may be situations where that's exactly what you want. Of course it could just be an unintended consequence of some other consideration.