array of references

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    array of references

    From



    the following portion is taken.

    "Mike Wahler" <mkwah...@mkwah ler.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

    #2
    Re: array of references

    subramanian100i n@yahoo.com wrote:
    From
    >

    >
    the following portion is taken.
    >
    "Mike Wahler" <mkwah...@mkwah ler.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


    Comment

    • James Kanze

      #3
      Re: array of references

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

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

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

      Comment

      Working...