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 introduces the relevant knowledge of "example Analysis of union members in Clearing 11". In the operation of actual cases, many people will encounter such a dilemma, so 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!
Union members of the class type
Earlier versions of C++ did not allow members of union to be members of class types that defined constructors or copy control members, and this restriction was removed in Clippers 11. But don't be happy too soon, because this type of union needs to call the constructor or destructor of the relevant class member as required when a value conversion involving a class type member occurs.
Suppose we have the following StrValue class. The purpose of this class is to output log information during object operations.
The code is a little long. Take your time. This class is mainly used to store strings. It contains three constructors, a destructor, and an assignment operator. The output operator is also overloaded to output the internal string. This class is allowed to be a member of the following union after Category 11.
The introduction of class type members for union brings a hassle: when switching valid members of union, you need to properly call the constructor and destructor of class members. For example, the following code:
The ival member is valid at first, and then you need to call the constructor using positioning new when you want to store the string; on the contrary, when you want to use the ival member again, you need to call the destructor of the sval member. If you do not do so, there is no guarantee that memory management will work properly.
Use classes to manage union members
The way to solve the above problems is the most basic feature of C++: encapsulation. We can encapsulate the union into another class.
We categorize the members of the SmartUnion class.
Two data members, one is the ut used to hold the type information, through which the SmartUnion class can determine which member is currently valid, and the other is the anonymous union, which contains integers and StrValue members, which is the protagonist.
Three constructors, one default constructor, and the other two accept integer and string input, respectively. Note that the SmartUnion here is just sample code, and you may have to add a lot of content when you actually use it, such as a copy constructor.
Three assignment operators provide the ability to assign values from integers, StrValue objects, and SmartUnion objects, respectively.
After some preparation, the usage is simple:
This is the end of the introduction of "an example Analysis of the members of union in Category 11". Thank you for your 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.
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.