Returning a Reference from a Function in C++ - dummies

Returning a Reference from a Function in C++

By John Paul Mueller, Jeff Cogswell

It’s possible to return a reference from a function. But be careful if you try to do this: You do not want to return a reference to a local variable within a function — because when the function ends, the storage space for the local variables goes away. Not good!

But you can return a reference to a global variable. Or, if the function is a member function, you can return a reference to a member variable.

For example, here’s a class that has a function that returns a reference to one of its variables:

class DigInto
{
private:
    int secret;
public:
    DigInto() { secret = 150; }
    int &GetSecretVariable() { return secret; }
    void Write() { cout << secret << endl; }
};

Notice the constructor stores 150 in the secret variable, which is private. The GetSecretVariable() function returns a reference to the private variable called secret. And the Write() function writes out the value of the secret variable. Lots of secrets here! And some surprises too. You can use this class like so:

int main(int argc, char *argv[])
{
    DigInto inst;
    inst.Write();
    int &pry = inst.GetSecretVariable();
    pry = 30;
    inst.Write();
    return 0;
}

When you run this, you see the following output:

150
30

The first line is the value in the secret variable right after the application creates the instance. But look at the code carefully: The variable called pry is a reference to an integer, and it gets the results of GetSecretVariable(). And what is that result?

It’s a reference to the private variable called secret — which means that pry itself is now a reference to that variable. Yes, a variable outside the class now refers directly to a private member of the instance! After that, you set pry to 30. When you call Write() again, the private variable will indeed change.

Does that seem like a bad idea? You make the variable private. And now the GetSecretVariable() function pretty much wiped out any sense of the variable actually remaining private. The main() function was able to grab a reference to it and poke around and change it however it wanted, as if it were not private! Trouble in C++ land!

That’s a problem with references: They can potentially leave your code wide open. Therefore think twice before returning a reference to a variable. Here’s one of the biggest risks: Somebody else might be using this code, may not understand references, and may not realize that the variable called pry has a direct link to the private secret variable.

Such an inexperienced programmer might then write code that uses and changes pry — without realizing that the member variable is changing along with it. Later on, then, a bug results — a pretty nasty one at that!

Because functions returning references can leave unsuspecting and less-experienced C++ programmers with just a wee bit too much power on their hands, use caution with references. No, you don’t have to avoid them altogether; just be careful. Use them only if you really feel you must. But remember also that a better approach in classes is to have member access functions that can guard the private variables.

However, references can be very powerful, provided you understand what they do. When you use a reference, you can easily modify another variable without having to go through pointers — which can make life much easier sometimes. So, please: Use your newfound powers carefully.