Friday, March 23, 2012

Reference variable in C++!!

C++ is a call by reference language. So it supports reference variables. A reference variable is a alias to other variable. The reference variable behaves same like a original variable it is referencing. Reference variable is like a const pointer that is implicitly dereferenced. These variables are declared using ampersand (&). Referance variable should not be NULL. It should refer to some value.

Simple exampe:

#include<iostream>
using namespace std;
main()
{
    int iVar = 10;
    int& rVar = iVar; // declaring the reference variable
    int& rVar1;  // illegal , reference variable should refer to some variable
    rVar=20;
    cout<<"ivar is "<<iVar<<endl;
    cout<<"rvar is "<<rVar<<endl;
}

Output:
ivar is 20
rvar is 20

Reference variable is same as normal variable, if you modify the normal , reference variable effects and vice-varsa.

Reference variable can't be redirected: Reference variable cant't be redirected to other variable. i.e. once assigned to a variable cant ba assigned to other variable. See the sample code below.

    int& rVar = iVar; // declaring the reference variable
    int iVar2 =30;
    rVar  = iVar2;  // it only assigns the value of iVar2 to rVAr


In the above code snippet, rVAr is a reference variable and is referencing to iVar. In the third statement, iVar2 is assigning to rVar. Here it will assign only value. And it will not change the reference variable. It will reference to iVar only.

Constant reference: C++ allows to use constant references. A constant reference will not allow you to change the value it referenced. 
    int iVar = 10;
    int const &cVar = iVar; // declaring constant reference variable
    cVar = 40; // illegal, cant change the constant referenc
    iVar = 40; // legal, can change normal variable



Uses of reference pointer:

1. Const reference used as function parameters:  As constant reference variables are read only access. It wont allow to modify the value. so passing value as function arguments, it will be vary useful. Constatn reference used in copy constructors for passing object as a argument.


2. Easy access to the nested data: If there are nested data like structure in a structure in as structure. to access the data , we need to use specify each structure variable with dot operator. Using refernce varaible , we can reduce this. See the sample code below.

#include<iostream>
using namespace std;

struct str
{
    int x;
};
struct str1
{
    struct str s;
    int x;
};

struct str2
{
    struct str1 s;
    int x;
};


main()
{

    struct str2 ins;
    ins.x=10;
    ins.s.s.x = 20;
    int &strRef = ins.s.s.x;
    strRef = 40;
    cout<<"strf ref is "<<strRef<<endl;
    cout<<"struct x is "<<ins.s.s.x<<endl;
}

Output:
strf ref is 40
struct x is 40

In the above code, structure str2 member is structrure str1, and its memeber is structure str. To access that , you need to specify all the structure variable with dot operator, instead of this, you can use reference variable to access it as shown in the code.




No comments:

Popular Posts