<分区>
要关闭选民,请帮助我改进问题以便重新打开:How can I improve this question so that it gets reopened?
赫伯萨特 wrote :
A base class destructor should be either public and virtual, or protected and nonvirtual.
根据该指南,如果您有一个带有公共(public)非虚拟析构函数的类,则不应将该类用作基类。
为什么不将其标记为 final
以强制执行?
但萨特也 wrote以下,暗示不需要使用 final
:
Re "uses of final are rarer" - well, they sort of are. I don’t know of many, and during standardization Bjarne repeatedly asked for examples of problems it solved and patterns where it should be used, and I don’t recall any major ones that stood out.
Scott Meyer 的 Effective C++,第 7 项中的另一条相关引述暗示 final
现在可用,应该使用它:
If you're ever tempted to inherit from a standard container or any other class with a non-virtual destructor, resist the temptation! (Unfortunately, C++ offers no derivation-prevention mechanism akin to Java's final classes or C#'s sealed classes.)
另一个数据点是 standard library has no types marked "final" ,但这样做的原因似乎是为了避免破坏代码。
这里有一个类似的问题,但不完全重复,因为它遗漏了“protected, nonvirtual”选项:Default to making classes either `final` or give them a virtual destructor?