C++核心准则C.122:需要完全隔离接口和实现时用抽象类作为接口‍

网友投稿 483 2022-11-12

C++核心准则C.122:需要完全隔离接口和实现时用抽象类作为接口‍

C++核心准则C.122:需要完全隔离接口和实现时用抽象类作为接口‍

C.122: Use abstract classes as interfaces when complete separation of interface and implementation is needed

C.122:需要完全隔离接口和实现时用抽象类作为接口‍

Reason(原因)

Such as on an ABI (link) boundary.

类似ABI边界的做法。

ABI:https://baike.baidu.com/item/ABI/10912305

Example(示例)

struct Device {     virtual ~Device() = default;     virtual void write(span outbuf) = 0;     virtual void read(span inbuf) = 0; }; class D1 : public Device {     // ... data ...     void write(span outbuf) override;     void read(span inbuf) override; }; class D2 : public Device {     // ... different data ...     void write(span outbuf) override;     void read(span inbuf) override; };

A user can now use D1s and D2s interchangeably through the interface provided by Device. Furthermore, we can update D1 and D2 in ways that are not binary compatible with older versions as long as all access goes through Device.

用户可以通过Device提供的接口自由地使用D1或D2的对象。除此之外,只要是通过Device访问,我们甚至可以将D1和D2更新为与旧版本不兼容的二进制形式。

Enforcement(实施建议)

-

原文链接

​​https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c122-use-abstract-classes-as-interfaces-when-complete-separation-of-interface-and-implementation-is-needed​​

觉得本文有帮助?欢迎点赞并分享给更多的人。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:log4j中logger标签中additivity属性的用法说明
下一篇:linux下apache+SVN搭建完美版
相关文章

 发表评论

暂时没有评论,来抢沙发吧~