In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about how to understand the initialization and assignment of C++ structures. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's have a look together with the editor.
1. Structure initialization
Structure is a commonly used custom construction type, which is a very common method of data packaging. There are many ways to initialize structure objects, which can be divided into sequential initialization, specified initialization and constructor initialization. If you have the following structure.
Struct A {int b; int c;}
(1) Sequential initialization is the most commonly used initialization method because it is simple to write, but compared with the specified initialization, the initialization order of data members cannot be changed, and the flexibility is poor, and the form of expression is not intuitive, so you can't see the values of each data member of struct at a glance.
An a = {1,2}
(2) there are two ways to specify initialization (Designated Initializer), one is through a dot plus an assignment symbol, that is, ".fieldname = value", and the other is through a colon, that is, "fieldname:value", where fieldname is the name of the member of the structure. The former is the initialization mode introduced by C99 standard, and the latter is an extension of GCC. Unfortunately, some compilers, such as Visual C++, do not support specified initialization.
/ / period + assignment symbol An a = {. B = 1,. C = 2}; / / colon An a = {bgroup 1, c 2}
The Linux kernel likes to initialize with .fieldname = value. Using specified initialization, an obvious advantage is that the initialization order and number of members are variable, and the expansibility is good. For example, when adding fields at the end of the structure, it avoids a large number of modifications caused by traditional sequential initialization.
(3) Constructor initialization is common in C++ code, because struct in C++ can be regarded as class, and structures can also have constructors, so we can initialize structure objects through structure constructors. Given a structure with a constructor:
Struct A {A (int bjinint c) {this- > bimb; this- > cantic;}; int b; int c;}
Then the initialization of the structure object can be like the initialization of the class object:
An a (1pl 2)
Note: if struct defines a constructor, it cannot be initialized with curly braces, that is, it can no longer be initialized with the specified initialization and sequence.
two。 Structural body assignment
Variable assignment and initialization are not the same. Initialization is completed when the variable is defined and is part of the variable definition. Assignment is the action taken when you want to change the value of the variable after the variable definition is completed. Or a given structure A:
Struct A {int b; int c;}
Note: structural variables cannot be assigned in curly braces, for example, the following assignment is not allowed.
An a, a.
The following is a list of common methods for assigning structural variables.
(1) use memset to empty the structure variables:
/ / initialize as the compiler defaults (if an is a variable in the global static storage, the default initialization is 0, and if it is a local variable on the stack, it is initialized to random values by default) An a; memset (& a Magne0CoSzeof (a))
(2) assign values to each structure member variable in turn:
An a; a. A. B. 1.
(3) use the existing structure variable to assign a value to another structure variable. In other words, structural variables can be assigned to each other.
An a = {1jue 2}; struct An a 1x * A1}; / / assign existing structural variables to A1.
There is an essential difference between initialization and assignment. Initialization is the first assignment when a variable is defined, and assignment is a change operation of the value after the definition. The concept is different, so the implementation is also different.
The above is how to understand C++ structure initialization and assignment. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.