Item 5. References Are Aliases, Not Pointers

A reference is another name for an existing object. Once a reference is initialized with an object, either the object name or the reference name may be used to refer to the object.

int a = 12;int &ra = a; // ra is another name for a--ra; // a == 11a = 10; // ra == 10int *ip = &ra; // ip points to a

References are often confused with pointers, perhaps because C++ compilers often implement references as pointers, but they are not pointers and do not behave like pointers.

Three major differences between references and pointers are that there are no null references, all references require initialization, and a reference always refers to the object with which it is initialized. In the previous example, ...

Get C++ Common Knowledge: Essential Intermediate Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.