O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required