用于两级以上继承的非虚拟接口习惯用法?
非虚拟接口惯用法描述了虚拟方法如何成为非公共定制点,并且公共方法是非虚拟的以允许基类始终控制定制点的调用方式。
这是一个优雅的习惯用法,我喜欢使用它,但是如果派生类本身就是基类,它如何工作
The non-virtual interface idiom describes how the virtual methods are nonpublic customisation points, and public methods are nonvirtual to allow the base class to control at all times how the customisation points are called.
This is an elegant idiom and I like to use it, but how does it work if the derived class is a base class in itself
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它有效,因为派生类可以重写基类的私有虚函数,即使基类函数重写其基类函数也是如此。
这是完全合法的:
It works, because the derived class can override a private virtual function of a base class, even if the base class function overrides its base class function.
This is perfectly legal:
派生类可以自行决定:
您可以通过实现虚函数来完全重写该方法。
您可以通过在派生类方法中的某个时刻调用“中间”类函数来扩充该方法。
如果这不是您想要的,您需要在“中间”类中显式设置它。 但我不会。 如果您发现自己需要这样做,则可能意味着您没有为基类提供足够的自定义点。
The derived class can decide for itself:
You can just override the method completely by implementing the virtual function.
You can augment the method by calling the 'middle' classes function at some point in your derived class method.
If that's not what you want, you need to set it up explicitly in the 'middle' class. I wouldn't though. If you find yourself desiring this, it probably means you didn't give the base class enough customization points.