Take the 2-minute tour ×

I just came across this line of code:

SDL_Color textColor = { 255, 0, 255 };

It made me wonder why it can be declared like an array. I thought it might've just ment the same as textColor(255,0,255); but it didnt work like that when I tried making my own class. Can anyone explain when this type of syntax is used? Where are the parameters going..?

share|improve this question

3 Answers 3

up vote 10 down vote accepted

It is aggregate initialization of a type, most likely a simple struct or class. For example,

struct Foo 
  int i,j;
  double x,y;

int main()
  Foo f = {1,2, 3.,4.};

Note since there is some confusion regarding structs, the above example would also work with a class, which in this case is identical to the struct:

class Foo 
  int i,j;
  double x,y;

In C++11 this type of initialization is extended to non-aggregate types under certain conditions.

for example

std::vector<int> v = {1,2,3,4,5};
share|improve this answer
+1 for first one to mention C++11 –  Bartek Banachewicz Aug 23 '12 at 12:02
Since a class aggegrates data members as well does it mean an object of a class can be initialized the same way? –  user1534664 Aug 23 '12 at 12:09
@user1534664 yes, if it satisfies the conditions for being an aggregate. In this sense, struct and class are identical. –  juanchopanza Aug 23 '12 at 12:10
@user1534664 I added some words about aggregate class. –  juanchopanza Aug 23 '12 at 12:14
Cheers mate! I understand this concept now :) –  user1534664 Aug 23 '12 at 12:17

All aggregates can be brace-initialized, which initializes each aggregate member with the matching item. If the list contains fewer items than there are aggregate members, the remaining elements are initialized as though from an empty brace list.

share|improve this answer
Deleted my answer as I am clearly mistaken on some point. From the standard: An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equalinitializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3). I think the point you were making is that even though it may be a struct it could have any of the listed features that prevent it from being an aggregrate? –  hmjd Aug 23 '12 at 12:11
Meh. Aggregates are structs in the usual informal usage. Sure, you can technically have struct-like classes and class-like structs. But there are also the connotations and reasons we usually choose class or struct besides the slight technical difference. –  aschepler Aug 23 '12 at 12:12
@hmjd struct and class are the same in this respect. They both can be aggregates, or not. I added a note to my answer. –  juanchopanza Aug 23 '12 at 12:15

SDL_Color is an aggregate (a struct with 4 members, in this case).

You can initialize an aggregate (not just an array) using an initializer list.

SDL_Color textColor = { 255, 0, 255 };

is the same as

SDL_Color textColor;
textColor.r = 255;
textColor.g = 0;
textColor.b = 255;
textColor.unused = 0;
share|improve this answer
I guess you mean 3 members, not 4 :) –  Kiril Kirov Aug 23 '12 at 12:02
@KirilKirov: Actually, it has 4 members. –  Mankarse Aug 23 '12 at 12:03
@KirilKirov: No, the 4th is Uint8 unused –  Andrey Aug 23 '12 at 12:04
Ah, sorry, my bad. I thought you're talking in general. –  Kiril Kirov Aug 23 '12 at 12:13

Your Answer


