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 does C++ try to avoid defining default actions?

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

Share

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

This article introduces the knowledge of "Why C++ tries to avoid defining the default operation". Many people will encounter this dilemma in the operation of actual cases. then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

C.20: avoid defining default actions whenever possible

Translator's note: the default actions are those that are automatically generated by the compiler by default, such as destructing, copying / moving constructs, copying moving assignment operators, and so on.

Reason (reason)

It's the simplest and gives the cleanest semantics.

This should be done with the simplest and cleanest semantics.

Example (sample)

Struct Named_map {public: / /... No default operations declared... private: string name; map rep;}

Named_map nm; / / default constructNamed_map nm2 {nm}; / / copy construct

Since std::map and string have all the special functions, no further work is needed.

Although std::map and string have all the special functions, they do not need this functionality in the snippet code.

Note (Note)

This is known as "the rule of zero".

This is the well-known "0 default operation rule".

Enforcement (implementation recommendations)

(non-mandatory) although the rules themselves are not mandatory, a good static analyzer should be able to find a way to improve the code to meet this guideline. For example, if a class contains (pointer, size) composite members and destructors that release pointers, it may be converted to some kind of vector.

This is the end of the content of "Why C++ tries to avoid defining default actions". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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