Connecting Tech Pros Worldwide Forums | Help | Site Map

array of references

subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
#1: Nov 21 '07
From

http://groups.google.com/group/comp....2e8424a1cfbd2b

the following portion is taken.

"Mike Wahler" <mkwah...@mkwahler.netwrote:

" 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

Victor Bazarov
Guest
 
Posts: n/a
#2: Nov 21 '07

re: array of references


subramanian100in@yahoo.com wrote:
Quote:
From
>
http://groups.google.com/group/comp....2e8424a1cfbd2b
>
the following portion is taken.
>
"Mike Wahler" <mkwah...@mkwahler.netwrote:
>
" 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


James Kanze
Guest
 
Posts: n/a
#3: Nov 22 '07

re: array of references


On Nov 21, 5:33 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

[...]
Quote:
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.
Quote:
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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Closed Thread