Solved

C++ reference question

Posted on 2014-12-17
6
150 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 51

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

734 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