Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C++ reference question

Posted on 2014-12-17
6
Medium Priority
?
168 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 53

Expert Comment

by:Rgonzo1971
ID: 40506474
HI,

Shouldn't it be

int& ref_n = n;

Regards
0
 
LVL 12

Accepted Solution

by:
Tej Pratap Shukla ~Dexter earned 668 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 35

Assisted Solution

by:sarabande
sarabande earned 668 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
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!

 
LVL 71

Expert Comment

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

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 664 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

963 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