Page 1 of 1

A way to get past "array of references" error

Posted: Wed Oct 17, 2007 1:15 pm
by earlz
ok, so I had started coding, and had all these tedious values for making an array of references so that rather than go *reg16[1], I could just go reg16[1] and prevent lots of typos and errors, and also make it look nicer....well, after about 10 minutes of programming it all in, and writing some test code, I go to compile and get a nice clear error: "Error: array of references"

why the crap can I not have an array of references, I can make a frikkin array of const pointers but I can't do it with references? wtf!?

Posted: Wed Oct 17, 2007 1:28 pm
by JamesM
I've got to say the only time I have ever used references is when I'm forced to when doing operator overloading.

Stick to the pointers I say! :twisted:

Posted: Wed Oct 17, 2007 1:36 pm
by earlz
Well, I usually like to stick to pointers, but with this, the pointers don't change or anything, and I'd like to treat it like just an array, but I think making a class and operator[] for it is a bit overkill and could be slower due to function calls, so I just wanted to do the references thing...

Re: A way to get past "array of references" error

Posted: Wed Oct 17, 2007 1:48 pm
by Alboin
hckr83 wrote:why the crap can I not have an array of references, I can make a frikkin array of const pointers but I can't do it with references? wtf!?
References aren't pointers. A pointer is the actual numerical identity of an element in memory; basically an integer. A reference, on the other hand, is the actual variable, that is, the variable itself is passed by the compiler instead of the value held within.

I believe that's what it is, at least, I think I do. (I'm not 100% sure.)

Posted: Wed Oct 17, 2007 1:50 pm
by Candy
JamesM wrote:I've got to say the only time I have ever used references is when I'm forced to when doing operator overloading.

Stick to the pointers I say! :twisted:
Stop using ancient pointers and use references whenever possible.

If you need some pointers on references, click here

Posted: Wed Oct 17, 2007 2:01 pm
by earlz
wow...someone else that reads xkcd...(I've done that in real life, btw...lol)

Posted: Wed Oct 17, 2007 2:20 pm
by Alboin
hckr83 wrote:wow...someone else that reads xkcd...(I've done that in real life, btw...lol)
I'm a more User Friendly sort of guy. Maybe I'll add it to me list though. It seems pretty funny.

Posted: Thu Oct 18, 2007 12:08 am
by Solar
Well actually I wouldn't even argue about pointers vs. references, but strongly suggest to use <vector> instead of fiddling with arrays... 8)

Posted: Thu Oct 18, 2007 7:19 am
by JamesM
Candy wrote:
JamesM wrote:I've got to say the only time I have ever used references is when I'm forced to when doing operator overloading.

Stick to the pointers I say! :twisted:
Stop using ancient pointers and use references whenever possible.

If you need some pointers on references, click here
You posted the same xkcd link twice. Did you mean to do that?

Posted: Thu Oct 18, 2007 9:44 am
by Brynet-Inc
JamesM wrote:You posted the same xkcd link twice. Did you mean to do that?
I think he was trying to be funny, Which as we all know... doesn't come naturally with him ;)

:lol: j/k!

Posted: Thu Oct 18, 2007 11:19 am
by Candy
JamesM wrote:
Candy wrote:
JamesM wrote:I've got to say the only time I have ever used references is when I'm forced to when doing operator overloading.

Stick to the pointers I say! :twisted:
Stop using ancient pointers and use references whenever possible.

If you need some pointers on references, click here
You posted the same xkcd link twice. Did you mean to do that?
I wasn't sure the first link would be noticed at all.

Posted: Thu Oct 18, 2007 9:35 pm
by earlz
good judgment...I figured the pointers like was just some kinda simple explanation of pointers...

Re: A way to get past "array of references" error

Posted: Fri Oct 19, 2007 9:16 am
by Craze Frog
Alboin wrote:
hckr83 wrote:why the crap can I not have an array of references, I can make a frikkin array of const pointers but I can't do it with references? wtf!?
References aren't pointers. A pointer is the actual numerical identity of an element in memory; basically an integer. A reference, on the other hand, is the actual variable, that is, the variable itself is passed by the compiler instead of the value held within.

I believe that's what it is, at least, I think I do. (I'm not 100% sure.)
References are a special kind of pointers made because the normal pointer system is so unsafe. On the other hand, references are too restrictive...
This can be seen by looking at the generated asm code for these functions:

Code: Select all

int CallByRef(int &ref) {
    return ref;
}

int CallByValue(int *ref) {
    return *ref;
}
The generated code is exactly the same.