In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use = default for copy control members in Craft 11". In daily operation, I believe many people have doubts about how to use = default for copy control members. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to use = default for copy control members". Next, please follow the editor to study!
The operation automatically generated by the compiler
Any experienced C++ programmer will know that the C++ compiler automatically generates some default actions:
Default constructor
Destructor function
Copy constructor
Assignment operator
Mobile constructor
Move assignment operator
These operations are divided into two groups: the default constructor itself is a group, and the other five are a group.
If the programmer defines any constructor, the compiler no longer generates the default constructor.
The last five operations are grouped into groups called copy control members, which handle the replication, assignment, and destruction of class objects (copies). Note that the copy here is a noun, not a verb. If the programmer defines any of the five members, the compiler will not automatically generate the other members.
The author's understanding
Why are we grouped in this way? There are several situations to discuss here.
Simple data class
At this point, the function of the class is similar to the structure of the C language, and the class itself has no function and does not even define the constructor. In this case, the C++ compiler automatically generates a default constructor, and the values of the data members perform default initialization.
Although the result of default initialization is not necessarily what the programmer wants, there is generally no problem: programmers assign additional values.
Functional classes without resource management
Compared with the simple data class, this analogy of the simple data class adds the corresponding internal logic, so there will be some internal relations between the data members. At this point, the default initialization often does not meet the requirements, and the solution is to define the constructor.
As a C++ compiler, once the programmer has defined the constructor, it can be considered that the default initialization can no longer meet the needs of the programmer and no longer generate the default constructor. This approach is safer in most cases.
If the programmer still wants the compiler to generate the default constructor after Clippers 11, it can be done by = default.
Since there is no requirement for resource management, this class does not have to define a destructor.
Functional classes that contain resource management
Once the class contains resources that need to be freed, such as memory, ports, files, etc., you need to define a destructor to release those resources. The destructor is called automatically when the object is destroyed, just in time to accomplish this task. Generally speaking, once resources need to be released in the destructor, other copy control members also need to deal with them accordingly.
Similar to the rules for automatically generating default constructors, if the programmer defines a copy control member, the compiler no longer automatically generates anything else.
Of course, there are situations where, like shallow copies, the copy control members generated by the compiler can meet the requirements, and you can use = default for the copy control members to require the compiler to generate some copy control members.
This is the title of this article.
Example
The doubt of the old bird
If you are an old C++ bird, maybe you will say that the C++ compiler I use is not like this about the generation rules of copy control members?
For this, please see the explanation of the authoritative books:
Unfortunately, the second rule is not fully enforced: for backward compatibility, the compiler automatically generates copy constructors and copy assignment operators even if the programmer defines a destructor. But this feature is already enabled in the ISO standard, and you can expect a modern compiler to warn against it.
At this point, the study on "how to use = default for copy control members in Category 11" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.