What Can Be Referenced?

Any object can be referenced, including user-defined objects. Note that you create a reference to an object, not to a class. You do not write this:

int & rIntRef = int;    // wrong

You must initialize rIntRef to a particular integer, such as this:

int howBig = 200;
int & rIntRef = howBig;

In the same way, you don't initialize a reference to a CAT:

CAT & rCatRef = CAT;   // wrong

You must initialize rCatRef to a particular CAT object:

CAT frisky;
CAT & rCatRef = frisky;

References to objects are used just like the object itself. Member data and methods are accessed using the normal class member access operator (.), and just as with the built-in types, the reference acts as an alias to the object.

Get Sams Teach Yourself C++ in 24 Hours, Third 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.