Are class members in c++ guaranteed to be contiguous?
I've tried running the following code with almost all popular c++ compilers, and all of them yield the result 4, which is the relative address of the variable y. Is that a coincidence, or is it guaranteed by the language specifications to be this way? Isn't it possible that the compiler will not make the members x and y contiguous with the class basic address/ contiguous with each other?
Please note that this thread does not answer this question.
#include <iostream>
using namespace std;
class A {
public:
void f(){
cout << &(this->y) << endl;
}
int x, y;
};
int main(int argc, const char* argv[])
{
A *a = 0;
a->f();
return 0;
}