K
Kuku
What is the difference between a reference and a pointer?
Kuku said:What is the difference between a reference and a pointer?
You had problems in answering it??
Kuku said:What is the difference between a reference and a pointer?
Kuku said:What is the difference between a reference and a pointer?
Noah said:http://www.parashift.com/c++-faq-lite/references.html
That is the best you will find on the comparison in the faq.
Martin said:I have programmed for a couple of months in C and it's this automatic
dereferencing when you pass by reference I find most confusing,
because here C and C++ doesn't work out the same way....
Default said:I don't understand what you mean. C has no references, and passing
pointers in C++ works the same way as in C. There is no "it" to work
out differently.
Alan said:One might try to explain references to a C programmer as "pointers
that automatically dereference themselves". (Yes, I know that isn't
accurate in all contexts.) Based on his comments, Martin Jørgensen is
likely working with that mental model.
Default said:Alan Johnson wrote:
Which would be fine, but what the heck does "because here C and C++
doesn't work out the same way" mean? Passing pointers works the same.
If he meant that pointers and references aren't the same, then yeah.
Exactly... And I can't get it out of my head, but perhaps it will go away
with time
Phlip said:Martin Jørgensen wrote;
The fix is "a reference is another name for an object; an alias for that
object".
By "object" we also mean items with built-in types...
Martin said:Default User wrote:
I believe in C you would get either a compiler error or warning if
you don't dereference the pointer when you want to access the value
of the pointer (in C).
That same thing is also a reference (in C++)
and that's the difference!
I mean: "Automatic dereferencing" like I wrote. When you write
something with & in front of it, you're actually passing the address
of that variable and not the value of the variable.
Sure, but what does that have to do with anything? What "difference"
between the languages are you talking about? There are differences
between pointers and references, but that's not a language difference,
other than C ain't got 'em (references).
Martin said:Default User wrote:
-snip-
If it isn't a language difference, what the hell is it then?
Default said:You tell me. What difference between the way pointers are handled have
you detected between the two languages.
I'll remind you again, references are not pointers. You're hung up on a
mythical automatic dereferencing that doesn't exist.
Kai-Uwe Bux said:Default User wrote: -snip-
No he isn't. He is just coming to terms with the fact that in C++ you can
write
hm = foo( bar );
and you are not able to tell wether bar is passed by value or by reference,
i.e., when you are calling a function, there is no indication of its side
effects on arguments. The difference between the languages that he is
struggling with is exactly that C++ has call by value *and* call by
reference -- and that the decision is made is at the point of declaration
not at the point of use.
that object".> The fix is "a reference is another name for an object; an alias for
Of course, C++ wouldn't be C++ if life didn't get more complicated: foo
could also be a function object and the call could have side effects there,
too.
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.