Solved

C++ reference question

Posted on 2014-12-17
6
152 Views
Last Modified: 2014-12-18
http://www.tutorialspoint.com/cplusplus/cpp_references.htm
says:

Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another object at any time.

int main()
{
    int n = 56;
    int m=34;
     int &ref_n = n;
     ref_n=m;

    cout<<ref_n<<endl;
    return 0;
}

Open in new window


But this code ouputs 34, why not 56?
0
Comment
Question by:Nusrat Nuriyev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40506474
HI,

Shouldn't it be

int& ref_n = n;

Regards
0
 
LVL 11

Accepted Solution

by:
Tej Pratap Shukla ~Dexter earned 167 total points
ID: 40506499
hello..
 
 &ref_n means setting ref_n's ADDRESS to n's ADDRESS means ref_n is referring to n.

 int &ref_n = n;

means value of n is changed with the value of ref_n.

and ref_n = m

m= 34

That's why output is 34 .
0
 
LVL 34

Assisted Solution

by:sarabande
sarabande earned 167 total points
ID: 40506677
to see that Dexter's answer is correct, check the value of n after the assignment. it is also 34. cause ref_n is only an alias of n.

Sara
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 70

Expert Comment

by:Qlemo
ID: 40506694
The reference is constant, not the value it refers to.
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40506712
Shouldn't it be  int& ref_n = n;
the position of '&' is arbitrary at any place between type and name. same applies for the asterisk * for a pointer variable.

putting it to the variable is more a c like attitude. putting it to the type is c++-like and shows better that you are well aware of the differences between a reference or pointer type to a normal type. I personally prefer the middle way:

int & ref_n = n;

Open in new window


Sara
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 166 total points
ID: 40506930
It might help to think of it this way...

'Under the hood', a reference is really just a pointer.  But the language automatically dereferences the pointer for you so that you treat it like a normal variable.

You have to also understand the difference in ...
  int &ref_n = n
... which sets what ref_n points to and ...
  ref_n = m
... which is now just treating ref_n as a normal variable.  It's just that ref_n and n are THE same variable.

Again, it might help to see the difference like this...
  &ref_n = n  //<-- Sets a pointer value
... which is setting what ref_n points to and ...
  ref_n = m  //<-- Set the value of what the pointer points to
... which is setting the value of ref_n references.

So ...
  &ref_n = n    
and
  ref_n = m
... are sort of analogus to
  p = &n;
and
  *p = n;
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question