473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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!

Apr 30 '06 #1
9 10547
"cheng" <ch*******@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
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.
Apr 30 '06 #2
In my response I used the variable name "a" a lot instead of the correct
variable name "i".
Apr 30 '06 #3
"cheng" <ch*******@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com
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);

--
John Carson
Apr 30 '06 #4
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?

Apr 30 '06 #5
thank you

Apr 30 '06 #6
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

Apr 30 '06 #7
hiiii

Apr 30 '06 #8
dnoo100 wrote:
thank you


One thanks the newsgroup by answering questions (and bouncing posts) here!

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Apr 30 '06 #9
"cheng" <ch*******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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.
May 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
2
by: N3TB1N | last post by:
Let me try again. I could use some help with this assignment, even though my teacher does not grade assignments.but because I need to know this stuff for a test very soon, but haven't been in...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
12
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
33
by: aaron | last post by:
I have a question in my class.. hoping to get some help I need to create a program that will print firstName middleName lastName then their initials User will input: John Smith Doe Output:...
2
by: Miro | last post by:
I am a pure beginner and reading 3 books at the same time trying to learn vb. I have created 2 forms in my "Solution" No where can I see in any of the books how you load / call one form from the...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.