It would seem, a reference is simply an alias, yet, adding reference-fields to a struct
, for example, increases the structure's size even when the reference is initialized at declaration as an alias for another field of the same structure.
For example:
#include <iostream>
using namespace std;
int
main(int, char **)
{
struct {
int integers[2];
} first;
struct {
int integers[2];
int &one = integers[0];
int &two = integers[1];
} second;
cout << sizeof first << " " << sizeof first.integers << " " <<
sizeof second << " " << endl;
return 0;
};
The above program prints: 8 8 24
here. The first two numbers I understand, the third -- no. Why does adding such references matter -- what is stored in that memory, that cannot be resolved at compile time? Unlike pointers, once declared, references cannot change by design anyway, can they? So why are they being stored?