mercredi 4 septembre 2019

Uniform initialization of dynamic array of structs

The code below prints garbage (or zeroes) if compiled with VC++ 2017 and "1122" if compiled with gcc or clang (https://rextester.com/JEV81255). Is it bug of VC++ or I'm missing something here?

#include <iostream>

struct Item {
    int id;
    int type;
};

int main()
{
    auto items = new Item[2]
    {
        { 1, 1 },
        { 2, 2 }
    };

    std::cout << items[0].id << items[0].type;
    std::cout << items[1].id << items[1].type;
}

Aucun commentaire:

Enregistrer un commentaire