new concept,is this a struct?

so playing around with SDL I noticed this, what exactly is going on here,I'm surprised I've never come across it before, we are creating an SDL_MessageBoxData named messageboxdata but in a strange way I have never seen before,why do we include everything in {} curly braces?

why not call it's constructor in the usual way?

1
2
3
4
5
6
7
8
9
10
11


const SDL_MessageBoxData messageboxdata = {
        SDL_MESSAGEBOX_INFORMATION, /* .flags */
        NULL, /* .window */
        "example message box", /* .title */
        "select a button", /* .message */
        SDL_arraysize(buttons), /* .numbuttons */
        buttons, /* .buttons */
        &colorScheme /* .colorScheme */
    };
Last edited on
Don't forget that SDL is a C library and C doesn't have constructors.

SDL_MessageBoxData is a struct and this syntax can be used to initialize structs in both C and C++. In C++ this is called aggregate initialization. https://en.cppreference.com/w/cpp/language/aggregate_initialization
It is no different than creating a SDL_Rect like SDL_Rect r{0,0,10,10};, perhaps you used a rect and never noticed that you created it like that!

And I can see why it could be convenient to save the settings (parameters) of a popup into a nice block of data so you can reuse it if you want a generic vague "you sure" prompt, and the data itself is quite big when you look at the minimal example on SDL, so perhaps it was the right choice to use a struct.

But other than that it is purely stylistic, from all that you know SDL could've been chose to make the renderer and window have a RendererData and WindowData because someone thought that the constructors (unlike C++, it's just a global function) had too many parameters, and just thrown into a struct.
Last edited on
Topic archived. No new replies allowed.