469,912 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,912 developers. It's quick & easy.

array of references

hello.
why can't you have an array of references ??? can someone enlighten me on
this ??
thanx.

Jul 23 '05 #1
4 9909
maadhuu wrote:
why can't you have an array of references ??? can someone enlighten me on
this ??


You can only create an array of objects. References are not objects.

V
Jul 23 '05 #2
* maadhuu:
why can't you have an array of references ??? can someone enlighten me on
this ??


It's simpler that way.

A reference cannot be assigned, and has no size.

If arrays of references were allowed they would therefore have to be treated
specially in every way.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3
maadhuu wrote:
why can't you have an array of references ???


Because references are not "things". They are not objects; you can't point
to them, take their addresses, etc.

Naturally, many situations implement them as invisible pointers. However, if
the C++ Standard allowed any syntax more that forced them to become
"invisible pointers", such law would limit the kinds of optimizations
compilers can subject them to. You can't point to a reference, and index
addressing is explicitely defined as a form of pointing-to.

The next question should be this: What benefit can you expect to derive from
an array of references. If you need to replace -> with ., then look to your
own interface, not to the Standard. Here's pseudo-C++ showing such an
interface:

class Things
{
private:
std::vector<Thing *> things;
public:
Thing & operator[] (int idx)
{
Thing * pThing = things[idx];
assert(pThing);
return *pThing;
}
};

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #4
In article
<b8******************************@localhost.talkab outprogramming.com>,
"maadhuu" <ma************@yahoo.com> wrote:
hello.
why can't you have an array of references ??? can someone enlighten me on
this ??
thanx.


You might look at std::tr1::tuple if your std::library supplies it, or
at boost::tuple if your std::lib doesn't. It may supply you with the
functionality you're looking for.

#include <tuple>
#include <iostream>

int main()
{
int i, j, k;
std::tr1::tuple<int&, int&, int&> int_refs = std::tr1::tie(i, j, k);
std::tr1::get<0>(int_refs) = 1;
std::tr1::get<1>(int_refs) = 2;
std::tr1::get<2>(int_refs) = 3;
std::cout << i << '\n';
std::cout << j << '\n';
std::cout << k << '\n';
}

1
2
3

-Howard
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kaptain524 | last post: by
9 posts views Thread by Timothy Madden | last post: by
10 posts views Thread by Fraser Ross | last post: by
104 posts views Thread by Leszek | last post: by
4 posts views Thread by None | last post: by
7 posts views Thread by heddy | last post: by
11 posts views Thread by rayreeves | last post: by
2 posts views Thread by subramanian100in | last post: by
31 posts views Thread by siddhu | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.