CHAPTER 6

image

References

References allow a programmer to create a new name for a variable. They provide a simpler, safer and less powerful alternative to pointers.

Creating References

A reference is declared in the same way as a regular variable, except that an ampersand is appended between the data type and the variable name. Furthermore, at the same time as the reference is declared it must be initialized with a variable of the specified type.

int x = 5;int& r = x; // r is an alias to xint &s = x; // alternative syntax

Once the reference has been assigned, or seated, it can never be reseated to another variable. The reference has in effect become ...

Get C++ 14 Quick Syntax Reference, Second Edition 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.