Connecting Tech Pros Worldwide Forums | Help | Site Map

array of references

maadhuu
Guest
 
Posts: n/a
#1: Jul 23 '05
hello.
why can't you have an array of references ??? can someone enlighten me on
this ??
thanx.


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: array of references


maadhuu wrote:[color=blue]
> why can't you have an array of references ??? can someone enlighten me on
> this ??[/color]

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

V
Alf P. Steinbach
Guest
 
Posts: n/a
#3: Jul 23 '05

re: array of references


* maadhuu:[color=blue]
> why can't you have an array of references ??? can someone enlighten me on
> this ??[/color]

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?
Phlip
Guest
 
Posts: n/a
#4: Jul 23 '05

re: array of references


maadhuu wrote:
[color=blue]
> why can't you have an array of references ???[/color]

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


Howard Hinnant
Guest
 
Posts: n/a
#5: Jul 23 '05

re: array of references


In article
<b8e1bebc469a46cc06c4176ad4b28249@localhost.talkab outprogramming.com>,
"maadhuu" <madhu_ranjan_m@yahoo.com> wrote:
[color=blue]
> hello.
> why can't you have an array of references ??? can someone enlighten me on
> this ??
> thanx.[/color]

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
Closed Thread