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(示例)
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:
如果确实需要分割处理对象,定义一个显式操作完成这个功能。这样可以避免读者困惑。例如:
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()};
Warn against slicing.
发现分割处理发出警告。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es63-dont-slice
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!