mardi 19 décembre 2017

Is there a difference between these ways of initialising a C array in C++?

Specifically I'm wanting to initialise all members of the array to zero, or nullptr;

struct Window{ int a;};


int main()
{
    Window* list[4] = { 0, 0, 0, 0 };
    Window* list2[4] = {0};
    Window* list3[4] = {};
    Window* list4[4]{ 0, 0, 0, 0 };
    Window* list5[4]{0};
    Window* list6[4]{};
}

I understand that when initialising at least one member to any value all the others are zero initialised, so if I do:

int list[4] = { 6 };

The first member becomes 6 and all the rest are zero-initialised. I'm confused however with:

int list[4]{0};
and
int list[4]{};

I assume that the empty squiggly brackets right after the declaration without an equals sign are what's called zero initialisation, as opposed to default initialisation, but so too is int list[4]{0}, isn't it? Does this involve an std::initializer_list behind the scenes or not? I thought these were only used for non-POD types, so std::initializer_list is not being used here?

Aucun commentaire:

Enregistrer un commentaire