C++核心准则C.126:抽象类通常不需要构造函数‍

2020-03-25 16:37:32 浏览数 (1)

C.126: An abstract class typically doesn't need a constructor

C.126:抽象类通常不需要构造函数‍

Reason(原因)

An abstract class typically does not have any data for a constructor to initialize.

抽象类通常不包含任何需要构造函数初始化的数据。

Example(示例)

代码语言:javascript复制
???

Exception(例外)

  • A base class constructor that does work, such as registering an object somewhere, may need a constructor.
  • 执行某些动作的基类的构造函数,例如将对象登录到某处,可能还是需要的。
  • In extremely rare cases, you might find it reasonable for an abstract class to have a bit of data shared by all derived classes (e.g., use statistics data, debug information, etc.); such classes tend to have constructors. But be warned: Such classes also tend to be prone to requiring virtual inheritance.
  • 也存在特别稀少的情况,你可能发现抽象类包含少量被所有派生类共享的数据也是合理的(例如,使用静态数据,调试信息等);这样的类有构造函数会更好些。但是需要注意的是:这样的类也可能要求虚继承。

Enforcement(实施建议)

Flag abstract classes with constructors.

对包含构造函数的抽象类进行提示。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#chierclass-designing-classes-in-a-hierarchy


觉得本文有帮助?请分享给更多人。

关注【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

0 人点赞