-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-2-class-initializer-lists.cpp
More file actions
258 lines (206 loc) · 4.1 KB
/
Copy path02-2-class-initializer-lists.cpp
File metadata and controls
258 lines (206 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <iostream>
using namespace std;
/*
1. Initializer lists are one of the most important C++ constructor concepts,
especially once you understand const, references, inheritance, and member objects.
An initializer list is the part after a constructor's :
*/
class Person {
private:
std::string name;
int age;
public:
Person(std::string n, int a) : name(n), age(a) {}
};
// : name(n), age(a) is the constructor initializer list.
/*
2. Why do we need it?
The most important thing to understand is: C++ objects have two different stages
C++ must first construct/initialize the members:
Person object
│
├── name → constructed
└── age → initialized
Only after all members have been initialized does the constructor body execute!
1. Construct name
2. Initialize age
3. Enter constructor body
*/
/*
3. Initializer list vs assignment
Consider:
*/
class Person_ {
string name;
int age;
public:
Person_(string n, int a) {
name = n;
age = a;
}
};
/*
You might think this means:
create name
create age
then:
name = n
age = a
So we're doing:
construct name
↓
assign n to name
Instead, with:
Person(std::string n, int a): name(n), age(a)
{
}
we do:
construct name directly with n
construct age with a
*/
/*** Mandatory Use Cases ***/
// Case-1: const members
class Person1 {
public:
const int age;
Person1(int a) : age(a) {}
};
/*
Person(int a)
{
age = a; // ❌
}
must be initialized when the object is created.
*/
// Case-2: References also require initializer lists
class Person2 {
int& age;
public:
Person2(int& a) : age(a) {}
};
// Case-3: Object that do not have default constructors
class Engine {
public:
Engine(int horsepower) {
}
};
class Car {
Engine engine;
public:
Car(int hp) : engine(hp) {}
// Car(int hp) { // too late }
};
// Case-4: Memeber Objects are constructed before the constructor body
class Engine_ {
public:
Engine_(int hp) {
cout << "Engine constructed\n";
}
};
class Car_ {
Engine_ engine;
public:
Car_() : engine(200)
{
cout << "Car constructed\n";
}
};
/*
Engine constructed
↓
Car constructor body
↓
Car constructed
The constructor body does NOT construct members.
The members have already been constructed by the time the body starts.
*/
// UseCase-1: Initializer lists are also used to initialize the base class.
class Animal {
public:
Animal(string name) {
}
};
class Dog : public Animal {
public:
Dog(string name) : Animal(name)
{
}
};
// UseCase-2: Initializer list can call different constructors
class Database {
public:
Database (string host, int port) {
}
};
class Application {
Database db;
public:
Application() : db("localhost", 5432) {
}
};
// UseCase-3: Performance!!!
// Compare:
class Person__ {
string name;
public:
Person__(const string& n) {
name = n;
}
};
/*
construct empty string
↓
assign n
*/
// With:
class Person___ {
string name;
public:
Person___(const string& n) : name(n) { // construct string directly from n
}
};
// Note-1: Initialization order — VERY important!
class Test {
int a;
int b;
public:
Test() : b(20), a(10)
{
}
};
/*
You might think:
b → 20
a → 10
Wrong.
C++ initializes members according to their declaration order:
int a; // first
int b; // second
Therefore:
a → 10
b → 20
*/
// Note-2: The initializer list is not actually "inside" the constructor body
class Student {
int age;
string name;
public:
Student(int a) : age(a) { // initialization phase
cout << "age is " << a << endl; // constructor body
}
};
/*
Object creation
│
▼
Initialize base classes
│
▼
Initialize members
│
▼
Execute constructor body
*/
int main() {
return 0;
}