Return by Reference to a Pointer, Huh?

Published 2012-09-27 on Farid Zakaria's Blog

Recap

Quick recap, for those who may be hazy on what a reference is in C++:

When a variable is returned by reference, a reference to the variable is passed back to the caller. The caller can then use this reference to continue modifying the variable, which can be useful at times. Return by reference is also fast, which can be useful when returning structs and classes.

In even plainer words, the compiler is not creating a copy of the variable onto the stack of the function but simply passing the reference (i.e. sort of similar to a pointer).

Here is the kicker and what is important to remember; in C++ everything is passed by value (copied) onto the function stack unless it has the '&' symbol denoting a reference.

Reference to Pointer

When is a reference to a pointer useful? Consider the example below.


void reassign(int * myInt){
    myInt = new int(7);
}
int main(){
    int * myInt = new int(5);
    reassign(myInt);
    std::cout << *myInt << std::endl;
    return 0;
}

What would you expect to be the output of the program? If you guessed 5 you'd be correct. The reason why it is 5 and not 7, is because even the pointer itself is copied onto the function stack and the reassignment only modifies that copy of the pointer's value and not the pointer from main's scope!

How to solve this dilemma?

Traditionally the way to solve this problem (most commonly seen for link list problems for instance), is to pass a Pointer to a Pointer. This way we are passing an extra level of indirection to the pointer and therefore any modification to the pointer would continue passed the function's scope. This works because now we are creating a copy of the address of the second level indirection. The example would change to become:


void reassign(int ** myIntPP){
    *myIntPP = new int(7);
}

Another way that solve this problem however is by simply passing a reference to a pointer like so:


void reassign(int* & myIntP){
    myIntP = new int(7);
}

In the return!

Up until this point, a lot of this should be common place in most code bases. What I found interesting however in the code I'm currently working on at my current job, are functions that return a reference to a pointer. Consider the example below:


class MyIntClass { 
public:
    MyIntClass(int value){
        myIntP = new int(value);
    }
    MyIntClass(){
        myIntP = new int(0);
    }
    int * & getMyInt() { 
        return myIntP;
    }      
private:
    int * myIntP;

};
int main(){
    MyIntClass myObject(5);
    int *& theInt = myObject.getMyInt();
    theInt = new int(10);
    std::cout << "theInt:" << *theInt << std::endl;
    std::cout << "myObject:" << *myObject.getMyInt() << std::endl;
    return 0;
}

The output of theInt and myObject would both be 10. The ability to do this seems odd as it is a good source of memory leaks and un-intuitive to me.

Can someone point me to some good use cases of how this can be useful?