定義時賦值
1
2
3
4
5
6
7
8
9
|
struct InitMember
{
int first;
double second;
char* third;
float four;
};
struct InitMember test = {-10,3.141590,"method one",0.25};
|
需要注意對應的順序,不能錯位。
定義後逐個賦值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
struct InitMember
{
int first;
double second;
char* third;
float four;
};
struct InitMember test;
test.first = -10;
test.second = 3.141590;
test.third = "method two";
test.four = 0.25;
|
因為是逐個確定的賦值,無所謂順序啦。
定義時亂序賦值(C風格)
這種方法類似於第一種方法和第二種方法的結合體,既能初始化時賦值,也可以不考慮順序;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
struct InitMember
{
int first;
double second;
char* third;
float four;
};
struct InitMember test = {
.second = 3.141590,
.third = "method three",
.first = -10,
.four = 0.25
};
|
這種方法經常使用,是很不錯的一種方式。
定義時亂序賦值(C++風格)
這種方法和前一種類似,網上稱之為C++風格,類似於key-value鍵值對的方式,同樣不考慮順序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
struct InitMember
{
int first;
double second;
char* third;
float four;
};
struct InitMember test = {
second:3.141590,
third:"method three",
first:-10,
four:0.25
};
|