Solved

C++ reference question

Posted on 2014-12-17
6
120 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
6 Comments
 
LVL 48

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 32

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 68

Expert Comment

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now