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 once think that the complier will sent 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 construct an temp object using the initialization list, copy the temp object to "item", and finally destruct the temp object.
My question is: Is that because I code on vs2017 that the complier doesn't send me an error? And what happened to the code above. Thank you for you time.
Aucun commentaire:
Enregistrer un commentaire