In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.