简体   繁体   English

课程默认是最终的吗?

[英]Should classes be final by default?

Quoting the Effective C++ (Scott Meyers), third edition, item 7: 引用Effective C ++(Scott Meyers),第三版,第7项:

Declare destructors virtual in polymorphic base classes. 在多态基类中声明析构函数是虚拟的。

This implies that classes intended to be inherited from should, at a minimum, have the destructor virtual. 这意味着要继承的类至少应该具有析构函数virtual。

When writing some applications/libraries, some classes (I would say not few) are not designed with the intention of being inherited from. 在编写一些应用程序/库时,有些类(我会说并不少)不是为了继承而设计的。 We usually rely on some conventions, in which one is not supposed to inherit classes that are not his own or without checking if it's safe to. 我们通常依赖于一些约定,其中一个不应该继承不属于他自己的类或者不检查它是否安全。

Now, a coding standard may require to write and design classes such that inheritance is always safe. 现在,编码标准可能需要编写和设计类,以便继承始终是安全的。 I feel like this might be too much. 我觉得这可能太多了。 C++11 has added the final keyword that makes sure classes are not inherited from. C ++ 11添加了final关键字,确保不继承类。 Would you recommend marking all classes that are not designed for inheritance as final by default? 您是否建议将默认情况下未设计为继承的所有类标记为final

This would make the compiler enforce what we have been doing by convention for ages. 这将使编译器强制执行我们多年来一直遵循的惯例。 But that convention might be considered well enough understood and easy to follow (especially since inheritance is usually avoided, with composition being preferred) such that it might be just another thing to care of while writing the code. 但是,可以认为该约定已经被充分理解并且易于遵循(特别是因为通常避免继承,并且优选组合),因此在编写代码时可能只是另一件事。

Just because a class does not have any virtual functions does not mean that inheriting from it does not make sense. 仅仅因为一个类没有任何虚函数并不意味着从它继承就没有意义了。 It just means it was probably not intended and that you have to be careful not to delete any instances of the derived objects through pointers to the base class. 它只是意味着它可能没有意图,你必须小心不要通过指向基类的指针删除任何派生对象的实例。 private inheritance would often be used in such a case, as an alternative to composition, to signal that inheriting from the base is purely an implementation detail and the derived class is not to be considered as having a "is-a" relationship with the base. private继承通常会在这种情况下使用,作为组合的替代,表示继承自base的纯粹是一个实现细节,并且派生类不被视为与base具有“is-a”关系。

Marking a class final is a great way to express that a class is not intended to be derived from, at all, by making it impossible. 标记一个类final是一种很好的方式来表达一个类本来不是要通过使它成为不可能而衍生出来的。 So if that's what you want, slap a final on the class. 因此,如果这是你想要的,一个巴掌final的类。

In some cases, marking a (derived) class final to prevent any further derivatives can also have the added benefit of making it easier for the compiler to devirtualize function calls, thus leading to better performance in some cases. 在某些情况下,标记(派生)类final以防止任何进一步的派生也可以具有使编译器更容易使函数调用虚拟化的额外好处,从而在某些情况下导致更好的性能。

In my opinion, final should (usually) be the default and you can then remove it later if you find that you need/want to derive from the class after all. 看来, final应该(通常)是默认值,然后如果你发现你需要/想要从类中派生出来,你可以稍后删除它。 But that's just opinion. 但这只是意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM