I'm a beginner to C++ (coming from Java).
I'm trying to wrap my head around normal variables, pointers, and references. Normal variables I get. Pointers too. References confuse me a bit.
I think I understand the idea behind references: firstly, they are 'automatically dereferenced' pointers. Instead of having to do *pointer = something
, pointer->doStuff()
(which is syntactic sugar for (*pointer).doStuff()
) and similar stuff, we can simply do reference = something
, reference.doStuff()
, etc.
Secondly, they are 'safer pointers', in that they can't be re-seated and you can't do arithmetic on them.
But there's one last thing I want to be sure about:
I'm pretty sure that where ever a reference variable appears in an expression or statement, it 'automatically' gets translated into the name of the variable it's referencing. So wherever I see the name of a reference, I can regard it as the name of the variable being referenced, and that would be correct.
Which means: you never operate on references, you always operate on the objects they reference. You can't store references, you always store the objects they reference. Etc.
Is this assumption accurate?