Introduction to struct in C++ 2-2

1 data structure

A data structure organizes a group of related data elements and uses them. The C++ language allows users to customize data structures in the form of structures ( struct ). A structure ( struct ) begins with the keyword struct , followed by the name of the structure and the body of the structure. A struct surrounded by curly braces forms a new scope. A semicolon must be written after the closing curly brace on the right side of the structure.

For the detailed introduction of structure ( struct ), please refer to " Introduction to struct in C++ 2-1 " " Introduction to C++ struct 2-1" .

2 The constructor of the structure

Each structure defines the way its objects are initialized. The structure controls the initialization process of its objects through one or several special member functions, which are called constructors . The task of the constructor is to initialize the data members of the class object, and the constructor is executed whenever an object of the class is created.

The constructor has the same name as the class, but has no return type. E.g

struct myStruct1{

int i_num;

string s_num;

myStruct1(int i, string s)

{

i_num = i;

s_num = s;

}

};

Among them, the function myStruct1 is the constructor of the custom structure myStruct1 . Use the following code to define an object of myStruct1 .

myStruct test_struct1 = myStruct1(1, “Hello”);

At this time, the value of i_num in test_struct is 1 ; the value of s_num is "Hello".

3 Constructor initializer list

The constructor initializer list is responsible for assigning values ​​to one or several data members of the newly created object. Add a colon between the parentheses of the constructor parameter list of the structure and the angle brackets of the structure, and add the names of the structure member variables after the colon. Each name is followed by parentheses. The parentheses contain the member variables. the initial value of .

struct myStruct2{

int i_num;

string s_num;

myStruct2():i_num(1), s_num("Hello")

{

}

};

Define the object of myStruct2 by the following code

myStruct2 test_struct2 = myStruct2();

At this time, the value of i_num of test_struct2 is 1 , and the value of s_num is "Hello".

4 Actual combat

4.1 Questions raised

A friend in the CSDN forum asked the meaning of the following code

struct File_not_found {

std::string filename;

File_not_found(const std::string& filename_ = std::string())

: filename(filename_) {}

};

struct Key_not_found {

std::string key;

Key_not_found(const std::string& key_ = std::string())

: key(key_) {}

};

The above code actually uses knowledge points such as custom structures and structure constructors.

4.2 Problem solving

The code mentioned in " 4.1 Questions" defines a structure named File_not_found , which contains a member variable filename of type string , which is used to save the file name. As mentioned in " 3 Constructor Initial Value List", the constructor of the structure contains the initialization list, in which the member variable filename is initialized to filename_ , and filename_ is the parameter of the constructor, this parameter has a default value, which is an empty string. The constructor of the File_not_found structure is actually equivalent to

File_not_found(const std::string& filename_ = std::string())

 {

filename = filename_;

}

If you use the following code to define the object of the File_not_found structure:

File_not_found myFile1 = File_not_found();

At this point, the value of the member variable filename of myFile1 is an empty string. If you use the following code to define the object of the File_not_found structure:

File_not_found myFile2 = File_not_found(“C:\\1.txt”);

At this time, the value of the member variable filename of myFile2 is "C:\\1.txt".

Next , a structure named Key_not_found is defined in the File_not_found structure. The structure also contains a member variable key of type string , which is used to store the key value. The constructor of this structure is defined in a similar way to the File_not_found structure.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324817545&siteId=291194637