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:
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.
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.
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.
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.
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.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
No comments:
Post a Comment