array of references

S

subramanian100in

From

http://groups.google.com/group/comp...e5e37fd194d/6e2e8424a1cfbd2b#6e2e8424a1cfbd2b

the following portion is taken.


" An object will occupy memory space (in
your program's memory area). A reference is simply another
name for the same memory area. 'Behind the scenes', your
C++ implementation might (and probably does, but isn't
required to) use memory to make the reference work, but
from your program's perspective, it does not occupy any memory."

My Question:
-------------------
From the above, I understand the following:
Because a reference does not occupy memory from the program's
viewpoint, we cannot have array of references.

Is my understanding correct ? Kindly explain

Thanks
V.Subramanian
 
V

Victor Bazarov

From

http://groups.google.com/group/comp...e5e37fd194d/6e2e8424a1cfbd2b#6e2e8424a1cfbd2b

the following portion is taken.


" An object will occupy memory space (in
your program's memory area). A reference is simply another
name for the same memory area. 'Behind the scenes', your
C++ implementation might (and probably does, but isn't
required to) use memory to make the reference work, but
from your program's perspective, it does not occupy any memory."

My Question:
-------------------
From the above, I understand the following:
Because a reference does not occupy memory from the program's
viewpoint, we cannot have array of references.

Is my understanding correct ? Kindly explain

Your understanding can be seen as correct. However, the situation
is simpler: arrays of references are prohibited by the language
(see Standard, [dcl.ref]/4). The Standard does not explain why no
arrays of referneces shall exist, just says there shall be none.

It's possible to speculate and find a logical explanation as to
why no arrays of references are allowed, but it's not really
a good use for our time.

V
 
J

James Kanze

[...]
Your understanding can be seen as correct. However, the situation
is simpler: arrays of references are prohibited by the language
(see Standard, [dcl.ref]/4). The Standard does not explain why no
arrays of references shall exist, just says there shall be none.

More generally, the standard says that references are not
objects. The fact that you cannot have an array of references
stems directly from that.

The fact that references are not objects means that they have
neither an address nor a size. (Taking the address of a
reference results in the address of the referred to object, and
sizeof a reference type is the size of the referred to type.) C
style arrays require pointer arithmetic to work, and pointer
arithmetic implies that the objects in the array have an address
and a size. (How could the array convert implicitly to the
address of the first element, if that element didn't have an
address.) The standard containers (std::vector, etc.) are
designed to contain objects as well. It's never stated as a
required concept that the objects be addressable, because by
definition, all objects are addressable. But they obviously
cannot be made to work if the elements aren't addressable or
don't have a known constant size.
It's possible to speculate and find a logical explanation as
to why no arrays of references are allowed, but it's not
really a good use for our time.

It's perhaps useful to understanding to show that the
restriction is coherent with the overall definition of
references, and not just an arbitrary restriction.

The choice that references not be objects is perhaps arbitrary,
but once the standard made that choice, a lot of other
restrictions follow automatically.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top