References in C + +

Source: Internet
Author: User

http://www.geeksforgeeks.org/references-in-c/

When a variable was declared as reference, it becomes an alternative name for a existing variable. A variable can declared as reference by putting ' & ' in the declaration.

#include <iostream>using namespace Std;int main () {  int x = ten;  Ref is a reference to X.  int& ref = x;  Value of X is now changed to  ref =;  cout << "x =" << x << Endl;  Value of X is now changed to +  x =;  cout << "ref =" << ref << Endl;  return 0;}

Output:

x = 20ref = 30

Following is one more example this uses references to swap both variables.

#include <iostream>using namespace Std;void swap (int& first, int& second) {    int temp = first;    first = second;    Second = temp;} int main () {    int a = 2, b = 3;    Swap (A, b);    cout << a << "" << B;    return 0;}

Output:



References vs Pointers
Both references and pointers can be used to the change local variables of one function inside another function. Both of them can also is used to save copying of the big objects when passed as arguments to functions or returned from FUNCTI ONS, to get efficiency gain.
Despite above similarities, there is following differences between references and pointers.

References is less powerful than pointers
1) Once A reference is created, it cannot being later made to reference another object; It cannot be reseated. This is often do with pointers.
2) References cannot be NULL. Pointers is often made NULL to indicate that they is not pointing to any valid thing.
3) A Reference must is initialized when declared. There is no such restriction with pointers

Due to the above limitations, references in C + + cannot be used for implementing data structures like Linked List, Tree, et C. In Java, references don ' t has above restrictions, and can be used to implement all data structures. References being more powerful in Java, is the main reason Java doesn ' t need pointers.

References is safer and easier to use:
1) Safer: Since references must is initialized, wild references like wild pointers is unlikely to exist. It is still possible to has references that don ' t refer to a valid location (see questions 5 and 6 in the below exercise )
2) Easier to use: References don ' t need dereferencing operator to access the value. They can used like normal variables. ' & ' operator is needed only at the time of the Declaration. Also, members of the An object reference can is accessed with dot operator ('. '), unlike pointers where arrow operator (-) is needed to access members.

Together with the above reasons, there is few places like copy constructor argument where pointer cannot is used. Reference must is used pass the argument in copy constructor. Similarly references must is used for overloading some operators like + +.



Exercise:
Predict the output of following programs. If there is compilation errors, then fix them.

Question 1

#include <iostream>using namespace Std;int &fun () {    static int x = ten;    return x;} int main () {fun    () = +;    cout << Fun ();    return 0;}



Question 2

#include <iostream>using namespace std;int fun (int &x) {    return x;} int main () {    cout << fun (ten);    return 0;}



Question 3

#include <iostream>using namespace Std;void swap (char * &str1, char * &str2) {  char *temp = str1;  str1 = str2;  str2 = temp;} int main () {  char *str1 = "GEEKS";  Char *str2 = "for GEEKS";  Swap (str1, str2);  cout<< "STR1 is" <<str1<<endl;  cout<< "STR2 is" <<str2<<endl;  return 0;}



Question 4

#include <iostream>using namespace Std;int main () {   int x = ten;   int *ptr = &x;   int &*ptr1 = PTR;}



Question 5

#include <iostream>using namespace Std;int main () {   int *ptr = NULL;   int &ref = *ptr;   cout << ref;}



Question 6

#include <iostream>using namespace Std;int &fun () {    int x = ten;    return x;} int main () {fun    () = +;    cout << Fun ();    return 0;}

References in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.