initialization
Structures are commonly used custom construction types and are a very common method of data packaging. There are many ways to initialize structure objects, which are divided into sequential initialization, specified initialization, and constructor initialization. If there is the following structure.
struct A { int b; int c; };
1.1 Sequential initialization
Sequential initialization is the most commonly used initialization method because it is relatively simple to write. However, compared with specified initialization, it is impossible to change the initialization order of data members, which is less flexible, and the manifestation is not intuitive, so you cannot see the values of each data member of struct at a glance.
A a = {1, 2};
1.2 Specify initialization
There are two ways to implement the Designated Initializer. One is to implement it by adding a dot symbol, that is,.fieldname=value
, the other is achieved through a colon, that isfieldname:value
where fieldname is the structure member name. The former is the initialization method introduced by the C99 standard, and the latter is an extension of GCC. Unfortunately, some compilers do not support specified initialization, such as Visual C++.
// Point + assignment symbolA a = {.b = 1, .c = 2}; // ColonA a = {b:1, c:2};
Linux kernel likes to use.fieldname=value
Initialization method, using specified initialization. An obvious advantage is that the member initialization order and number are variable, and it has good scalability. For example, when adding fields at non-end of the structure, it avoids a large number of modifications brought about by traditional sequential initialization.
1.3 Constructor initialization
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 the constructor of the structure. Given a structure with a constructor:
struct A { A(int b,int c) { this->b=b; this->c=c; }; int b; int c; }
Then the initialization of the structure object can be like the initialization of the class object:
A a(1,2);
Notice:struct If the constructor is defined, it cannot be initialized with braces, that is, it cannot be initialized with specified initialization and sequential initialization.
Assignment
The assignment and initialization of a variable are different. Initialization is completed when the variable is defined and belongs to the part of the variable definition. Assignment is an operation taken when you want to change the variable value after the variable definition is completed. Or given structure A:
struct A { int b; int c; };
Notice:The assignment of structure variables cannot be assigned in braces, for example, the following assignment is not allowed.
A a; a = {1,2}; // Incorrect assignment
The following lists common structure variable assignment methods.
(1) Use memset to empty the structure variable:
// Initialize the compiler default method (if a is a variable in the global static storage area, the default is 0, and if it is a local variable on the stack, the default is initialized to a random value)A a; memset(&a,0,sizeof(a));
(2) Assign values to each structure member variable in turn:
A a; = 1; = 2;
(3) Use the existing structure variable to assign values to another structure variable. In other words, structure variables can be assigned to each other.
A a = {1,2}; struct A a1; a1 = a; // Assign existing structure variable to a1
3. Summary
Initialization and assignment are essentially different. Initialization is the first assignment when defining a variable, and assignment is the change operation of the value after the definition. It is different in concepts, so the implementation is also different.
Therefore, when we perform operations on the struct variable, we must clearly know whether to initialize or assign actions in order to take different measures.
This is the end of this article about the implementation of C++ struct initialization and assignment. For more related contents of C++ struct initialization and assignment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!