Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Why not split the processing objects in C++?

Shulou Source: shulou.com Published: 2022-06-01 19:29:54 10月02日 Update

This article mainly explains "Why not divide the processing object in C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn why not divide the processing objects in C++!

ES.63: do not split processing objects

Reason (reason)

Segmentation means that the assignment or initialization of an object is only part of the object-- in most cases, it results in an error, because the object is intended to be processed as a whole. In rare cases, you do need to split up, but such code can be difficult to understand.

Example (sample)

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

Because the center and radius are not copied from c to s, it produces meaningless results. The first protection forbids the assignment of the base class.

Alternative (optional)

If you do need to split the processing object, define an explicit operation to accomplish this function. This can avoid readers' 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 ()}; Enforcement (implementation recommendation)

Warn against slicing.

The split process is found to issue a warning.

Thank you for your reading. The above is the content of "Why not split the processing object in C++". After the study of this article, I believe you have a deeper understanding of the problem of why not split the processing object in C++. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Processing object Clearing situation Learning content confusion Code function Radius reason that is suggestions ideas meaning measures totality articles is in more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker OPPO Reno NVidia Shulou Tech Info