473,408 Members | 1,743 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,408 software developers and data experts.

About C++ Reference

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

Jul 28 '07 #1
5 1280

<an*******@gmail.comwrote in message
news:11*********************@j4g2000prf.googlegrou ps.com...
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.
}
Jul 28 '07 #2
On Fri, 27 Jul 2007 22:52:56 -0700 in comp.lang.c++, an*******@gmail.com
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.

Jul 28 '07 #3
an*******@gmail.com 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;
}
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
Jul 28 '07 #4
Tak
On Jul 28, 1:52 pm, and.28...@gmail.com 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;
i=5 ri=5
ri =10;
i=10,ri=10
std::cout << i << " " << ri << std::endl;

}
Jul 28 '07 #5
On Jul 28, 12:07 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<and.28...@gmail.comwrote in message

news:11*********************@j4g2000prf.googlegrou ps.com...
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.

}
thanks a lot for this

Jul 28 '07 #6

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
6
by: Johnny Hansen | last post by:
Hello, I've been trying to implement smart pointers in C++ (combined with a reference counter) because I want to do some memory management. My code is based on the gamedev enginuity articles,...
23
by: guru.slt | last post by:
Hi, see this code: auto_ptr<int> int_auto_p(new int(3)); auto_ptr<int> int_auto_p2(int_auto_p.get()); Then, both auto_pointer will own the same object. That will violate the single...
7
by: Gui Lloyd | last post by:
I have a problem with performance in IE. The script above works quite fine when the table has a small number of elements, but, when the table has 2500 elements, when I click in the checkbox of the...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
9
by: Alvin Bruney | last post by:
The more knowledgable I get about this .net world, the more questions I have. ..NET uses pass by reference for all objects....uhhh I mean pass by value. (Couldn't resist this jab) Consider a...
3
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
35
by: rebeccatre | last post by:
hi can Variant archiving setTimout('.. capability be done without using it? :-)
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.