Is the following well defined behaviour, and does it behave as one would expect:
class A {
int x;
int &y = x;
int *z = &x;
}
Additionally, is there anything that I have to be careful of when doing something similar? (see update) Is it important that y comes after x?
Can I do it with an array like so:
class A {
int arr[2];
int &y = arr[0];
int *z = &(arr[1]);
}
All of this seems to work fine, but I'm concerned that this is implementation dependent/not portable.
Edit/Update
I was using this to replace a group of member variables with an array, while maintaining backwards compatibility. So, I'm going from something like this:
class A {
int x0;
int x1;
}
to
class A {
int x[2];
int &x0 = x[0];
int &x1 = x[1];
}
Since asking this question, I have come across an example of something to be wary of when using it like this: x0 and x1 do not behave the same as before if they appear in initializer lists. They cannot be initialized to an integer literal, and if they are initialized to anything at all, x0 will not refer to x[0] and/or x1 will not refer to x[1].
I would post this as an answer, but I still don't know the answer to my main question: Are any of the given scenarios implementation dependent/undefined behaviour?