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

References as an Alternative to Pointers

Listing 12.2 solves the problem of making extra copies and, thus, saves the calls to the copy constructor and destructor. It uses constant pointers to constant objects, thereby solving the problem of the called function making impermissible changes to the objects passed in as parameters. The method is still somewhat cumbersome, however, because the objects passed to the function are pointers.

Because you know the parameters will never be null, it would be easier to work with the function if references were passed in rather than pointers. Listing 12.3 rewrites Listing 12.2 to use references rather than pointers.

Listing 12.3. Passing References to Objects
 0: //Listing 12.3 1: // Passing references to ...

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