• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++: What's the difference between a pointer and a reference?

gopunk

Lifer
so pointers contain a memory address right? what do references contain? i know they're an "alias" but how does that work?
 
well, they're both objects that refer indirectly to another object.

the big difference is in the amount of control

you need to dereference a pointer with the * operator but you don't need to dereference & with any operator

it actually makes a bit of a difference when coding stuff like overloaded operators

of course then you get into null and const pointers.

In short, it doesn't really make a difference. Usually, it works out fine. Besides, use java and avoid all this crap 🙂

[edit]

just thought of something else. references can't be null. Pointers can be. That makes a difference. Say you need to pass an object to a function without allocating it any space. References have no null reference. This helps if you want to use an object in a function without testing if it exists. It saves a little hassle but these little things are easy to miss.
 
Actually, in C++, pointers and references are NOT objects.

A pointer is an integer that holds any memory address, including "null."
A reference is merely a pointer that holds the memory address of the beginning of an object or variable. You may pass anything by reference by using the "&."

A pointer is an actual datatype, whereas a reference is just a memory address.
 
Back
Top