Array of References

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neelesh

    Array of References

    Hi all,
    References are (almost always) implemented as constant pointers.
    We can certainly have an array of constant pointers.
    But then why we cannot have array of references?

    I can think of atleast two places where such a thing 'might' be used,
    though I am not clear whether it makes sense to have such a usage -

    1) Function call
    int f (int& a[]); // a is an array of integer references

    2) simple definition :
    void foo()
    {
    const int& p[] = { 2,3,4,5}; // p is an array of references to const
    integers.
    }


    Thanks in advance,
    Neelesh

  • Victor Bazarov

    #2
    Re: Array of References

    On Fri, 04 Nov 2005 08:19:11 -0800, Neelesh wrote:[color=blue]
    > References are (almost always) implemented as constant pointers.[/color]

    No. Or, rather, what makes you think that, anyway?
    [color=blue]
    > We can certainly have an array of constant pointers.[/color]

    Yes.
    [color=blue]
    > But then why we cannot have array of references?[/color]

    Because. References are not objects, we can only have arrays of objects.
    Same reason why we don't have arrays of functions.
    [color=blue]
    > I can think of atleast two places where such a thing 'might' be used,
    > though I am not clear whether it makes sense to have such a usage -
    > [...][/color]

    If you're not clear whether it makes sense, you should probably take
    a hint from the language creators: it likely has no sense, that's why it
    isn't there.

    V

    Comment

    • Andrey Tarasevich

      #3
      Re: Array of References

      Neelesh wrote:[color=blue]
      > ...
      > References are (almost always) implemented as constant pointers.
      > We can certainly have an array of constant pointers.
      > But then why we cannot have array of references?
      > ...[/color]

      The behavior of the language construct has absolutely nothing to do with the way
      it is "(almost always) implemented". References in C++ represent certain
      concept. And this concept, in its original form, does not include the
      possibility of having arrays of references.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      Working...