views:

960

answers:

6
+8  A: 

References are not objects. They don't have storage of their own, they just reference existing objects. For this reason it doesn't make sense to have arrays of references.

If you want a light-weight object that references another object then you can use a pointer. You will only be able to use a struct with a reference member as objects in arrays if you provide explicit initialization for all the reference members for all struct instances. References cannot be default initalized.

Edit: As jia3ep notes, in the standard section on declarations there is an explicit prohibition on arrays of references.

Charles Bailey
References are same in their nature as constant pointers, and therefore they take up some memory (storage) to point to something.
inazaruk
Not necesserily. The compiler might be able to avoid storing the address, if it's a reference to local object for instance.
EFraim
Not necessarily. References in structures usually take up some storage. Local references often don't. Either way, in the strict standard sense, references are not objects and (this was new to me) named references aren't actually _variables_.
Charles Bailey
Well, those are compiler-dependent optimizations. In general case references do take some memory.
inazaruk
Yes, but this is an implementation detail. An _object_ in the C++ model is a typed region of storage. A reference is explicitly not an object and there is no guarantee that it takes up storage in any particular context.
Charles Bailey
+4  A: 

An array is implicitly convertable to a pointer, and pointer-to-reference is illegal in C++

EFraim
It is true you cannot have a pointer to a reference, but this is not the reason that you cannot have an array of references. Rather they are both symptoms of the fact that references are not objects.
Richard Corden
+1  A: 

A reference object has no size. If you write sizeof(referenceVariable), it will give you the size of the object referenced by referenceVariable, not that of the reference itself. It has no size of its own, which is why the compiler can't calculate how much size the array would require.

Carl Seleborg
+15  A: 

C++ Standard 8.3.2/4:

There shall be no references to references, no arrays of references, and no pointers to references.

Kirill V. Lyadvinsky
What more is there to say?
polyglot
+1  A: 

Consider an array of pointers. A pointer is really an address; so when you initialize the array, you are analogously telling the computer, "allocate this block of memory to hold these X numbers (which are addresses of other items)." Then if you change one of the pointers, you are just changing what it points to; it is still a numerical address which is itself sitting in the same spot.

A reference is analogous to an alias. If you were to declare an array of references, you would basically be telling the computer, "allocate this amorphous blob of memory consisting of all these different items scattered around."

Dan Tao
+1  A: 

Because like many have said here, references are not objects. they are simply aliases. True some compilers might implement them as pointers, but the standard does not force/specify that. And because references are not objects, you cannot point to them. Storing elements in an array means there is some kind of index address (i.e., pointing to elements at a certain index), and that is why you cannot have arrays of references because you cannot point to them.

Youse boost::reference_wrapper, or boost::tuple instead. or just pointers.

navigator