Exercise
Using our first version of Sales_data from § 2.6.1 (p. 72),
explain the following initialization. Identify and fix any problems.
My code:
#include <iostream>
#include <string>
using namespace std;
struct Sales_data
{
string bookNo;
unsigned sold_units = 0;
double revenue = 0.0;
};
int main()
{
Sales_data item = { "978-1234567",25,15.99 };
cout << item.bookNo << ' ' << item.sold_units << ' ' << item.revenue << endl;
system("pause");
}
I first thought that the complier will give me an error, since there are in-class initial value to prevent "Sales_data" to be a aggregate class. But it does not.
After running the code, I think that the complier constructs a temp object using the initialization list, copy the temp object to "item", and finally destruct the temp object.
My question is: Is it because I code on Visual Studio 2017 that the complier doesn't send me an error? And what happened to the code above.