You are initializing a reference from a temporary. If you want instances of struct Y
to hold an actual string, rather than just a reference, you need a member variable of type string, not reference to string.
Q1. Why would s1 be uninitialized when it has already been initialized in the constructor initialization list?
It was initialized, but to be a reference to a string that no longer exists.
Q2. Why don't I get the same error if I use string s1; instead of the reference?
If the class contains a string, then it doesn't contain a reference to a string that exists.
What string do you think the reference is a reference to?