A way to get past "array of references" error

Programming, for all ages and all languages.
Post Reply
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

A way to get past "array of references" error

Post 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!?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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:
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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...
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

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

Post 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.)
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

wow...someone else that reads xkcd...(I've done that in real life, btw...lol)
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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)
Every good solution is obvious once you've found it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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!
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

good judgment...I figured the pointers like was just some kinda simple explanation of pointers...
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

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

Post 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.
Post Reply