-
Notifications
You must be signed in to change notification settings - Fork 12
/
aggregate_initialization.cpp
59 lines (47 loc) · 1.08 KB
/
aggregate_initialization.cpp
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
#include <iostream>
class entity {
public:
int x;
std::string y;
};
class intArray {
public:
int size;
int *data;
intArray(int size) {
size = size;
data = new int[size];
}
~intArray() { delete[] data; }
intArray(std::initializer_list<int> list) {
size = list.size();
data = new int[list.size()];
// std::initializer_list doesn't provide a subscript operator
int count{0};
for (auto element : list) {
data[count] = element;
++count;
}
}
};
int main() {
// Aggregate initialization (Initializations using public attributes)
entity entity1{2011, "name"};
entity entity2 = {2011, "name"};
/*
initializer_list
so far the only way to use our array is like this:
*/
intArray array1(4);
array1.data[0] = 7;
array1.data[1] = 3;
array1.data[2] = 5;
array1.data[3] = 6;
/*
with initializer_list, we can send an array directly:
*/
intArray array2({1, 2, 3, 4, 5, 6, 7, 8});
std::cout << array2.data[0] << std::endl;
std::cout << array2.data[1] << std::endl;
std::cout << array2.data[2] << std::endl;
}