Problem with initializing structure via new operator

Viewed 79

Below are two variants of my program

struct customer{
    char fullname[35];
    double payment;
};

int main()
{
    customer alex{"Alex", 15};
    return 0;
}

struct customer{
    char fullname[35];
    double payment;
};

int main()
{
    customer* alex = new customer {"Alex", 15};
    return 0;
}

The first one works fine, but the second raises error:could not convert '{"Alex", 15}' from 'brace-enclosed initializer list' to 'customer'. What's the issue?

1 Answers

I think the code is fine, but some compilers may complain.

Try instead:

customer* alex = new auto(customer{"Alex", 15});
Related