C++: what are free-standing references used for?
c++, reference
Solution
It is useful to replace a long expression to an object by a shorter reference and make the code more readable. For example:
const int &SphereRadius = Configuration::getInstance()->sphere->radius;
Whenever the configuration changes simultaneously (e.g. in another thread), your reference is updated.
The code you have shown is just a simple possibility of a greater tool. Your example in many cases is meaningless as you understood it before. The main goal of these kind of references is aliasing an object.
Passing objects by reference to a function and the ability to modify the referring object without the confusions of pointers.
Using them in ranged-based loops to modify the iterating item in a container.
Shortening an expression by a simpler one in some cases. and more... .
Problem
In which situation would you want to define a reference to some piece of memory? For example: ``` const int & r = 8; ``` as opposed to simply writing: ``` int r = 8; ```