前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则ES.63​:不要分割处理对象

C++核心准则ES.63​:不要分割处理对象

作者头像
面向对象思考
发布2020-05-29 14:57:07
3220
发布2020-05-29 14:57:07
举报

ES.63: Don't slice

ES.63:不要分割处理对象

Reason(原因)

Slicing -- that is, copying only part of an object using assignment or initialization -- most often leads to errors because the object was meant to be considered as a whole. In the rare cases where the slicing was deliberate the code can be surprising.

分割指的是在赋值或初始化对象是只处理对象一部分--多数情况下会导致错误,因为对象本来希望作为一个整体被处理。极少情况下确实需要分割处理,但是这样的代码会很难理解。

Example(示例)

代码语言:javascript
复制
class Shape { /* ... */ };
class Circle : public Shape { /* ... */ Point c; int r; };

Circle c {{0, 0}, 42};
Shape s {c};    // copy construct only the Shape part of Circle
s = c;          // or copy assign only the Shape part of Circle

void assign(const Shape& src, Shape& dest) {
    dest = src;
}
Circle c2 {{1, 1}, 43};
assign(c, c2);   // oops, not the whole state is transferred
assert(c == c2); // if we supply copying, we should also provide comparison,
                 // but this will likely return false

The result will be meaningless because the center and radius will not be copied from c into s. The first defense against this is to define the base class Shape not to allow this.

由于中心和半径不会从c复制给s,因此产生没有意义的结果。第一种保护措施禁止基类的赋值操作。

Alternative(可选项)

If you mean to slice, define an explicit operation to do so. This saves readers from confusion. For example:

如果确实需要分割处理对象,定义一个显式操作完成这个功能。这样可以避免读者困惑。例如:

代码语言:javascript
复制
class Smiley : public Circle {
    public:
    Circle copy_circle();
    // ...
};

Smiley sm { /* ... */ };
Circle c1 {sm};  // ideally prevented by the definition of Circle
Circle c2 {sm.copy_circle()};
Enforcement(实施建议)

Warn against slicing.

发现分割处理发出警告。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es63-dont-slice


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

关注微信公众号【面向对象思考】轻松学习每一天!

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ES.63: Don't slice
    • Enforcement(实施建议)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档