Valhalla Legends Forums Archive | C/C++ Programming | Pointers vs. References

AuthorMessageTime
Mephisto
[quote author=Myndfyre link=board=30;threadid=6875;start=0#msg60874 date=1085012521]

You have a pointer to a reference? Seems a bit odd. They typically serve almost the same purpose, and you probably don't need to do that. I believe a reference might be the best way to go. Of course, I could be entirely wrong.
[/quote]

It's not uncommon when you'll have to use a pointer to a pointer, and if you don't need the functionality a pointer provides over a reference you can use a reference for simplicity if you prefer. Personally, I prefer pointers over references, but most of the C/C++ programmers disagree with this.
May 20, 2004, 1:29 AM
Myndfyr
[quote author=Mephisto link=board=30;threadid=6875;start=0#msg60900 date=1085016589]
[quote author=Myndfyre link=board=30;threadid=6875;start=0#msg60874 date=1085012521]

You have a pointer to a reference? Seems a bit odd. They typically serve almost the same purpose, and you probably don't need to do that. I believe a reference might be the best way to go. Of course, I could be entirely wrong.
[/quote]

It's not uncommon when you'll have to use a pointer to a pointer, and if you don't need the functionality a pointer provides over a reference you can use a reference for simplicity if you prefer. Personally, I prefer pointers over references, but most of the C/C++ programmers disagree with this.
[/quote]

Yeah, I know about multiple-indirection. It just seemed odd at first glance, that have a reference to a pointer. -_-
May 20, 2004, 1:46 AM
Maddox
Normally references to pointers are used when you want to initialize a pointer with an address.

For example

[code]
// untested
void func(int *& p) { p = new int; }
int main() { int *ptr; func(ptr); return 0; }
[/code]
May 20, 2004, 1:49 AM
Mephisto
[quote author=Myndfyre link=board=30;threadid=6875;start=0#msg60903 date=1085017619]
[quote author=Mephisto link=board=30;threadid=6875;start=0#msg60900 date=1085016589]
[quote author=Myndfyre link=board=30;threadid=6875;start=0#msg60874 date=1085012521]
[quote author=Maddox link=board=30;threadid=6875;start=0#msg60907 date=1085017792]
Normally references to pointers are used when you want to initialize a pointer with an address.

For example

[code]
// untested
void func(int *& p) { p = new int; }
int main() { int *ptr; func(ptr); return 0; }
[/code]

[/quote]

You have a pointer to a reference? Seems a bit odd. They typically serve almost the same purpose, and you probably don't need to do that. I believe a reference might be the best way to go. Of course, I could be entirely wrong.
[/quote]

It's not uncommon when you'll have to use a pointer to a pointer, and if you don't need the functionality a pointer provides over a reference you can use a reference for simplicity if you prefer. Personally, I prefer pointers over references, but most of the C/C++ programmers disagree with this.
[/quote]

Yeah, I know about multiple-indirection. It just seemed odd at first glance, that have a reference to a pointer. -_-
[/quote]

As far as I know, a reference to a pointer is illegal. He has a pointer to a reference which is fine.
May 20, 2004, 2:12 AM
Maddox
No, I think you've got those two mixed up.
May 20, 2004, 2:33 AM
Zeller
My guess would be taking in a referenced pointer as a paramater is the only way to change a pointers address (the adress the pointer points to) and make it stay changed beyond scope of the function. After reading mephistos reply im now confused, so can some one clarify this.

[quote author=j0k3r link=board=30;threadid=6875;start=0#msg60841 date=1084999838]
I believe two slashes is used to represent the slash character in a string, as the slash character is the escape character.
[/quote]

I never learned about that. that would explain why /n, /t, /0 ect only take up one char though.
May 20, 2004, 2:34 AM
Zeller
[quote author=Maddox link=board=30;threadid=6875;start=0#msg60926 date=1085020436]
No, I think you've got those two mixed up.
[/quote]

shouldnt it work either way. A reference should have the same memory address as its original
May 20, 2004, 2:37 AM
Maddox
Well a pointer to a reference would be int&* which I think is illegal.
May 20, 2004, 2:41 AM
Zeller
[quote author=Maddox link=board=30;threadid=6875;start=0#msg60935 date=1085020875]
Well a pointer to a reference would be int&* which I think is illegal.
[/quote]

Now I see, I thought int&* and int*& were the same thing for some reason. Although I dont see why a reference to a pointer would be illegal, I cant argue with the compiler :)
May 20, 2004, 2:51 AM
Mephisto
[quote author=Maddox link=board=30;threadid=6875;start=0#msg60926 date=1085020436]
No, I think you've got those two mixed up.
[/quote]

Yea, you're right. I got it mixed up. A pointer to a reference is illegal (&*). A reference to a pointer is legal (*&).
May 20, 2004, 2:59 AM
Zeller
[quote author=Mephisto link=board=30;threadid=6875;start=15#msg60939 date=1085021986]
[quote author=Maddox link=board=30;threadid=6875;start=0#msg60926 date=1085020436]
No, I think you've got those two mixed up.
[/quote]

Yea, you're right. I got it mixed up. A pointer to a reference is illegal (&*). A reference to a pointer is legal (*&).
[/quote]

&* is just bad syntax for initializing a referance. its like saying:
[code]
& int myRef=whatever;
[/code]
If you use the currect syntax, what stops you from assigning a pointer to a reference (of another pointer)? I hate being a C++ noob :(
May 20, 2004, 4:31 AM
Moonshine
[quote author=Zeller link=board=30;threadid=6875;start=15#msg60955 date=1085027511]
[quote author=Mephisto link=board=30;threadid=6875;start=15#msg60939 date=1085021986]
[quote author=Maddox link=board=30;threadid=6875;start=0#msg60926 date=1085020436]
No, I think you've got those two mixed up.
[/quote]

Yea, you're right. I got it mixed up. A pointer to a reference is illegal (&*). A reference to a pointer is legal (*&).
[/quote]

&* is just bad syntax for initializing a referance. its like saying:
[code]
& int myRef=whatever;
[/code]
If you use the currect syntax, what stops you from assigning a pointer to a reference (of another pointer)? I hate being a C++ noob :(
[/quote]

That's the point of a reference to a pointer? You want to assign it to another pointer, and keep a reference to it.
May 20, 2004, 4:35 AM
Eibro
These discussions should probably be taken to a seperate thread. The poor guy asked for help on his program, not a discussion on the semantics of dereferencing.
May 20, 2004, 5:06 AM
Myndfyr
In any case, a reference to a pointer makes sense. A pointer to a reference does not, because then you're only dealing with one level of indirection when you really want two.
May 20, 2004, 5:18 AM
Moonshine
Well if he wants to get some serious help, he should take myndfire's advice about the code tags and such. :P
May 20, 2004, 3:49 PM
Mephisto
Why was this topic pinned?
May 21, 2004, 1:14 AM

Search