Quiz 49
Anonymous
2,215 views
description:
"An aggregate is an array or a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions." Aggregates can be initialized by "brace-enclosed, comma separated list of initializer-clauses for the members of the aggregate, written in increasing subscript or member order." Static data members are skipped during this type of initialization so 3.14 initializes d and not sd in this example "If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be value-initialized". So in this example i is value-initialized to 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
class Foo
{
public:
char c;
static double sd;
double d;
int i;
};
int main(int argc, char** argv)
{
Foo f = { 72, 3.14 };
std::cout << f.c + f.d + f.i << std::endl;
return 0;
}
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.