Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why should C++ keep the consistency of the default operation?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "why C++ should maintain the consistency of the default operation". In daily operation, I believe many people have doubts about why C++ should maintain the consistency of the default operation. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "why C++ should maintain the consistency of the default operation". Next, please follow the editor to study!

C.22: keep the default operation consistent

Reason (reason)

The default operation is conceptually a tightly coordinated set of processing. Their semantics are interrelated. Users will be surprised if copy / move constructions and copy / move assignments do logically different things; users will be surprised if constructors and destructors do not provide a consistent idea for resource management; users will be surprised if copy and move operations do not correspond to constructors and destructors.

Example, bad (negative example)

Class Silly {/ / BAD: Inconsistent copy operations

Class Impl {

/ /...

}

Shared_ptr p

Public:

Silly (const Silly& a): P {a.p} {* p = * a.p;} / / deep copy

Silly& operator= (const Silly& a) {p = a.p;} / / shallow copy

/ /...

}

These operations (copy construction and assignment) have different semantics about the copy (deep copy and shallow copy). This can lead to confusion and mistakes.

Enforcement (implementation recommendations)

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report