joe_04_04 joe_04_04 - 1 month ago 16
C++ Question

Passing by Reference and using reference operator

In my first c++ programming class, we didn't learn what the reference operator was doing when passing variables as reference, we just learned that using the reference operator in the formal parameter list meant that the variable was directly changing the corresponding variable in main. But now, I'm trying to understand what's really happening, now that I'm in the second programming class at my university. Consider this code:

#include <iostream>
using namespace std;

void Reference(int & Cool, int Neato);

int main()
int Awesome=10;
int *P = & Awesome;
int Neat=20;

Reference(Awesome, Neat);

cout << *P << endl;

void Reference(int & Cool, int Neato)

The first time the reference operator is being used, I'm giving the pointer *P the address of variable Awesome. This makes total sense to me. I'm getting a value for P and the value is the address of Awesome. After that, I pass Awesome to the function by reference, which I understand what's happening, but how is it read by the compiler?

I used the variable Neat as an example of what I asking. Neat is being passed by value, so I assume the compile sees it sort of as:

Neato = Neat;

But how is passing Awesome by reference read by the compiler? Following the same logic as above, I would assume:

& Cool = Awesome;

But syntactically, and logically, this makes no sense at all. Especially when compared to the first example of (which makes total sense):

int *Pointer = & Awesome;

So, I'm just trying to figure out exactly what's going on. I know that the variables passed by reference are being directly changed when their corresponding function variable changes, but what is happening in the background?


First, let's get some ground rules out of the way. Not directly related to your question, but these ground rules need to be established.

The '&' operator is overloaded, and means two different things in two different contents.

int foo=0;

int *bar=&foo;

Here, '&' is an address operator. It means take the address of some other object, and this becomes a pointer to the object.

void foo(int &bar);

Here, '&' declares a reference. Here, '&' means a reference declaration. It's a somewhat of a nuanced concept, but the difference is important.

Now, with that out of the way, to answer your question:

A reference is really a "hidden pointer", in a manner of speaking. To create a reference, the compiler generates the same code it does when it implements the address operator, and the reference, internally, is just a pointer.

void foo(int &);

// ...

int bar=0;


This can be thought of as being analogous to:

void foo(int *);

// ...

int bar=0;


This is logically equivalent.

Now, for the second part of the answer. Any time a reference is used, it is as of the dereference operator, the '*' operator, is applied to the internal pointer that the reference actually is:

int foo(int &bar)
    int foobar=bar;

This is analogous to:

int foo(int *bar)
    int foobar=*bar;

In conclusion:

A) When a new reference is created, it's as if an address operator is used on the referenced object.

B) When an existing reference is used, it's as if a dereference operator ius used on the actual pointer to the referenced object.

This should help you understand what happens when references are passed around.