About C++ Reference

A

and.280.c

hello m newbie at here now i have a problem with the "Reference" in "C+
+". i am following the Book("C++ Primer,Fourth Edition")
there is The Question

#include<iostream>
int main()
{
int i, &ri = i;
i = 5; ri =10;
std::cout << i << " " << ri << std::endl;
}


and the compiler return the answer 10 10
can there anyone who can tell me about it
how compiler take the value and return it
Andry
 
J

Jim Langston

hello m newbie at here now i have a problem with the "Reference" in "C+
+". i am following the Book("C++ Primer,Fourth Edition")
there is The Question

#include<iostream>
int main()
{
int i, &ri = i;
i = 5; ri =10;
std::cout << i << " " << ri << std::endl;
}


and the compiler return the answer 10 10
can there anyone who can tell me about it
how compiler take the value and return it
Andry

To format your program a little better (IMO)

#include<iostream>
int main()
{
int i;
// i is now an integer
int &ri = i;
// ri is a reference to an integer, in this case i. Whenever we deal
with ri
// it is just as if we are dealing with the orignal variable i
i = 5;
// That assigned the value of i to 5
ri =10;
// That assigned the value of i (which ri is a reference to) to 10.
std::cout << i << " " << ri << std::endl;
// i and ri both refer to the same variable i, which has 10 in it.
}
 
D

David Harmon

On Fri, 27 Jul 2007 22:52:56 -0700 in comp.lang.c++, (e-mail address removed)
wrote,
hello m newbie at here now i have a problem with the "Reference" in "C+
+". i am following the Book("C++ Primer,Fourth Edition")
there is The Question

#include<iostream>
int main()
{
int i, &ri = i;
i = 5; ri =10;
std::cout << i << " " << ri << std::endl;
}


int &ri = i; // means that ri is another name for variable i.
 
D

Default User

hello m newbie at here now i have a problem with the "Reference" in
"C+ +". i am following the Book("C++ Primer,Fourth Edition")
there is The Question

#include<iostream>
int main()
{
int i, &ri = i;
i = 5; ri =10;
std::cout << i << " " << ri << std::endl;
}


and the compiler return the answer 10 10
can there anyone who can tell me about it
how compiler take the value and return it
Andry


A reference creates an alias for the assigned variable. Your code was
effectively:

i = 5;
i =10;

std::cout << i << " " << i << std::endl;


See now?



Brian
 
T

Tak

hello m newbie at here now i have a problem with the "Reference" in "C+
+". i am following the Book("C++ Primer,Fourth Edition")
there is The Question

#include<iostream>
int main()
{
int i, &ri = i;
i = 5;

i=5 ri=5
 
A

and.280.c

To format your program a little better (IMO)

#include<iostream>
int main()
{
int i;
// i is now an integer
int &ri = i;
// ri is a reference to an integer, in this case i. Whenever we deal
with ri
// it is just as if we are dealing with the orignal variable i
i = 5;
// That assigned the value of i to 5
ri =10;
// That assigned the value of i (which ri is a reference to) to 10.
std::cout << i << " " << ri << std::endl;
// i and ri both refer to the same variable i, which has 10 in it.

}

thanks a lot for this
 

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,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top