防止后代覆盖方法
在 C++ 中(2017 年 11 月 14 日,我不在乎),我有一个基类 B
,它将方法 x
公开为 protected
。
在派生自 B
的 D1
类中,我想防止(D1
的)进一步的后代调用 x().现在,如果我要覆盖
D1
中的 x
,我只需将其标记为 final
。
但是,如果 D1
不覆盖 x()
,但 D1
仍想对后代隐藏它怎么办?我怎样才能做到这一点?
In C++ (11/14/17, I don't care), I have a base class B
which exposes method x
as protected
.
In class D1
, which derives from B
, I want to prevent further descendants (of D1
) from calling x()
. Now, if I were overriding x
in D1
, I would simply mark it as final
.
But, what if D1
does not override x()
, yet D1
still wants to hide it from descendants? How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过更改程序并覆盖
D1
中的x
。您可以通过调用它来委托给基类版本。By changing the program and overriding
x
inD1
. You can just delegate to the base class version by calling it.