References

P

Pelle Beckman

Hello,

I've seen almost everyone here talk about
references instead of pointers (as I understand it).
Anyone knows a good tutorial och can explain
the concept of references?

-- Pelle
 
J

Jonathan Bartlett

I've seen almost everyone here talk about
references instead of pointers (as I understand it).
Anyone knows a good tutorial och can explain
the concept of references?

You can think of a reference as a pointer that cannot change where it is
pointing, but is used syntactically as if it were just a value.

Things to note:

1) Since passing by reference actually passes a pointer, any
modifications done in the function will affect the original value.
2) For large structs, passing by reference prevents you from having to
copy the data structure when passed.
3) If you don't need to do pointer fiddling, the syntax of references is
much easier than that of pointers.

Basically, a reference is a pointer with all of the "pointer-y" stuff
hidden from you.

Jon
 
M

Matthias Kaeppler

Jonathan said:
You can think of a reference as a pointer that cannot change where it is
pointing, but is used syntactically as if it were just a value.

Things to note:

1) Since passing by reference actually passes a pointer, any
modifications done in the function will affect the original value.
2) For large structs, passing by reference prevents you from having to
copy the data structure when passed.
3) If you don't need to do pointer fiddling, the syntax of references is
much easier than that of pointers.

Basically, a reference is a pointer with all of the "pointer-y" stuff
hidden from you.

Jon

I find your description of references highly misleading. What you
describe is a const pointer, not a reference. A reference doesn't store
an address, it's just an alias for an object. That's all.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,664
Latest member
RoseannBow

Latest Threads

Top