A beginner's question about C++'s & operator

C

cheng

I was trying to figure out how & work, so I wrote the following code

#include <iostream>
using namespace std;

void print(int &);

int main()
{
int i = 10;
int &t = i;

cout<<"t = "<<t<<endl;
cout<<"&t = "<<&t<<endl;
cout<<"&i = "<<&i<<endl;
print(t);
cout<<"t = "<<t<<endl;

return 0;
}


void print(int &x)
{
x = 6;
cout<<"x = "<<x<<endl;
}

The output is:
x = 10
&x = 0012FF7C
&i = 0012FF7C
x = 6
i = 6

My questiont is:
int &t = i; what is this line of code doing?
Assigning an integer value to the address of t?

Also, I modified the print() a little bit like this:

void print(int x)
{
x = 6;
cout<<"x = "<<x<<endl;
}
Here is the new output:
x = 10
&x = 0012FF7C
&i = 0012FF7C
x = 6
i = 10

To me, it looks like the modified code is passing by value and the
original one is passing by reference. But my question is, in the
modified code, I did say "int &t = i;" and I passed t to print(int x).
How could a function takes an int as a parameter accept an reference?

Thank you very much for your help!
 
J

Jim Langston

cheng said:
I was trying to figure out how & work, so I wrote the following code

& has two different things it does depending on how it's used.

vartype & varname;
declares varname as a reference. A reference is like a pointer but you can
use . instead of -> and other differences.

&varname;
takes the address of varname.
#include <iostream>
using namespace std;

void print(int &);

int main()
{
int i = 10;
int &t = i;

This declares t as a reference to an integer and has it point to the
variable i.
cout<<"t = "<<t<<endl;

This will display the contents of what t is pointing to, in this case i, so
should display 10.
cout<<"&t = "<<&t<<endl;

This would display the address of where t is pointing to is stored (the
address of i). (Not positive, I would think it would display the address of
t, but your output shows otherwise)
cout<<"&i = "<<&i<<endl;

This would display the address of where the data for the variable i is
stored.
print(t);

This calls the function print which takes a reference to an integer. Since
t is already a reference it will be passed as a refernce to a.
cout<<"t = "<<t<<endl;

The variable a was changed by the call to print. t points to a, so this
will display the modified contents of a which is now 6.
return 0;
}


void print(int &x)
{
x = 6;

Since x is a reference, this changes the contents of the variable passed in
itself.
cout<<"x = "<<x<<endl;

This will display the contents of the variable a, since x is a reference to
it.
}

The output is:
x = 10
&x = 0012FF7C
&i = 0012FF7C
x = 6
i = 6

My questiont is:
int &t = i; what is this line of code doing?
Assigning an integer value to the address of t?

Also, I modified the print() a little bit like this:

void print(int x)
{
x = 6;
cout<<"x = "<<x<<endl;
}
Here is the new output:
x = 10
&x = 0012FF7C
&i = 0012FF7C
x = 6
i = 10

To me, it looks like the modified code is passing by value and the
original one is passing by reference. But my question is, in the
modified code, I did say "int &t = i;" and I passed t to print(int x).
How could a function takes an int as a parameter accept an reference?

It's automatically converted by the compiler.
Thank you very much for your help!

You're welcome.
 
J

Jim Langston

In my response I used the variable name "a" a lot instead of the correct
variable name "i".
 
J

John Carson

cheng said:
I was trying to figure out how & work, so I wrote the following code

#include <iostream>
using namespace std;

void print(int &);

int main()
{
int i = 10;
int &t = i;

cout<<"t = "<<t<<endl;
cout<<"&t = "<<&t<<endl;
cout<<"&i = "<<&i<<endl;
print(t);
cout<<"t = "<<t<<endl;

return 0;
}


void print(int &x)
{
x = 6;
cout<<"x = "<<x<<endl;
}

The output is:
x = 10
&x = 0012FF7C
&i = 0012FF7C
x = 6
i = 6

My questiont is:
int &t = i; what is this line of code doing?

It declares that t is a reference to i, which means that doing anything with
t is equivalent to doing it with i (the efficiency of doing something with t
may not be identical to doing it with i, but otherwise there is no
difference). t is an "alias" for i. Thus

t = 5;

has the same effect as

i = 5;
Assigning an integer value to the address of t?

& has two different roles depending on context. One is the "address of"
operator. The other is in the declaration of references. The two different
roles of & really have nothing to do with one another. When you see

int &t = i;

think "t is a reference to i" and forget all about addresses.
Also, I modified the print() a little bit like this:

void print(int x)
{
x = 6;
cout<<"x = "<<x<<endl;
}
Here is the new output:
x = 10
&x = 0012FF7C
&i = 0012FF7C
x = 6
i = 10

To me, it looks like the modified code is passing by value and the
original one is passing by reference.
Correct.

But my question is, in the
modified code, I did say "int &t = i;" and I passed t to print(int x).
How could a function takes an int as a parameter accept an reference?

Because, as I stated above, a reference is an alias. Anything you do with t
is equivalent to doing it with i. Thus calling

print(t);

is like calling

print(i);
 
C

cheng

Thanks a lot Jim!

But I still don't understand why does the compiler covert an reference
to an int when it supposes to take an reference as a parameter?
 
J

Jim Langston

cheng said:
Thanks a lot Jim!

But I still don't understand why does the compiler covert an reference
to an int when it supposes to take an reference as a parameter?

What do you mean? The compiler will convert between an int and a reference
as needed by the call. If you have one and need the other for a function it
will automatically be converted.

Please give me an example of what you mean.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top