array of references

M

maadhuu

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

Victor Bazarov

maadhuu said:
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
 
A

Alf P. Steinbach

* 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.
 
P

Phlip

maadhuu said:
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;
}
};
 
H

Howard Hinnant

maadhuu said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top