From syntactic POV, a reference is an alias for an existing object. From the semantic POV, this link behaves like a pointer with several problems (annulment, ownership, etc.) And an object-like syntax is added. From a practical POV, prefer links if you don’t need to say “no object”. (Resource ownership is not a preference for pointers, as this should be done using smart pointers.)
Refresh . Here's another difference between references and pointers that I forgot about: a temporary object (rvalue) associated with a constant reference will have a lifespan extended to the lifespan of the link:
const std::string& result = function_returning_a_string();
Here, the temporary function returned by the function is bound to result and will not cease to exist at the end of the expression, but will exist until result dies. This is nice, because in the absence of references to rvalue and overloads based on them (as in C ++ 11), this allows you to get rid of one unnecessary copy in the above example.
This rule is introduced specifically for const references, and there is no way to achieve this with pointers.
sbi
source share