Reference Properties and Oddities

Using reference arguments has several twists you need to know about. First, consider Listing 8.5. It uses two functions to cube an argument. One takes a type double argument, and the other takes a reference to double. The actual code for cubing is purposefully a bit odd to illustrate a point.

Listing 8.5. cubes.cpp

// cubes.cpp -- regular and reference arguments#include <iostream>double cube(double a);double refcube(double &ra);int main (){    using namespace std;    double x = 3.0;    cout << cube(x);    cout << " = cube of " << x << endl;    cout << refcube(x);    cout << " = cube of " << x << endl;    return 0;}double cube(double a){    a *= a * a;    return a;}double refcube(double &ra){    ra *= ra * ra; ...

Get C++ Primer Plus 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.