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
Solved

C++ reference question

Posted on 2014-12-17
6
137 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 50

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 33

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 69

Expert Comment

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

808 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