• Welcome to Valhalla Legends Archive.
 

Pointers vs. References

Started by Mephisto, May 19, 2004, 08:29 PM

Previous topic - Next topic

Mephisto

Quote from: Myndfyre on May 19, 2004, 07:22 PM

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.

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.

MyndFyre

Quote from: Mephisto on May 19, 2004, 08:29 PM
Quote from: Myndfyre on May 19, 2004, 07:22 PM

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.

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.

Yeah, I know about multiple-indirection.  It just seemed odd at first glance, that have a reference to a pointer.  -_-
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Maddox

Normally references to pointers are used when you want to initialize a pointer with an address.

For example


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

asdf.

Mephisto

#3
Quote from: Myndfyre on May 19, 2004, 08:46 PM
Quote from: Mephisto on May 19, 2004, 08:29 PM
Quote from: Myndfyre on May 19, 2004, 07:22 PM
Quote from: Maddox on May 19, 2004, 08:49 PM
Normally references to pointers are used when you want to initialize a pointer with an address.

For example


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



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.

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.

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

As far as I know, a reference to a pointer is illegal.  He has a pointer to a reference which is fine.

Maddox

No, I think you've got those two mixed up.
asdf.

Zeller

#5
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 from: j0k3r on May 19, 2004, 03:50 PM
I believe two slashes is used to represent the slash character in a string, as the slash character is the escape character.

I never learned about that. that would explain why /n, /t, /0 ect only take up one char though.

Zeller

Quote from: Maddox on May 19, 2004, 09:33 PM
No, I think you've got those two mixed up.

shouldnt it work either way. A reference should have the same memory address as its original

Maddox

Well a pointer to a reference would be int&* which I think is illegal.
asdf.

Zeller

Quote from: Maddox on May 19, 2004, 09:41 PM
Well a pointer to a reference would be int&* which I think is illegal.

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  :)

Mephisto

Quote from: Maddox on May 19, 2004, 09:33 PM
No, I think you've got those two mixed up.

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

Zeller

Quote from: Mephisto on May 19, 2004, 09:59 PM
Quote from: Maddox on May 19, 2004, 09:33 PM
No, I think you've got those two mixed up.

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

&* is just bad syntax for initializing a referance. its like saying:

& int myRef=whatever;

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  :(

Moonshine

Quote from: Zeller on May 19, 2004, 11:31 PM
Quote from: Mephisto on May 19, 2004, 09:59 PM
Quote from: Maddox on May 19, 2004, 09:33 PM
No, I think you've got those two mixed up.

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

&* is just bad syntax for initializing a referance. its like saying:

& int myRef=whatever;

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  :(

That's the point of a reference to a pointer?  You want to assign it to another pointer, and keep a reference to it.

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.
Eibro of Yeti Lovers.

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Moonshine

Well if he wants to get some serious help, he should take myndfire's advice about the  code tags and such.   :P