Showing posts with label reference. Show all posts
Showing posts with label reference. Show all posts

Disadvantages of references in Cpp

References can not be re initialized. They always refer to first referent.
 Reference arithmetic is not allowed.  Operation effects on actual referent. 
#include<iostream>
using namespace std;
int main()
{
int i=10;
int& j = i;
int & k =j;

cout<<"i: "<<i<<endl;
cout<<"j: "<<j<<endl;
cout<<"k: "<<k<<endl;

cout<<"&i: "<<&i<<endl;
cout<<"&j: "<<&j<<endl;
cout<<"&k: "<<&k<<endl;

//reference once initialized can not be reinitialed. point to first referent only.
int m=10;
int n=20;
int &x=m;
cout<<"x="<<x<<endl;//10
x=n;//this is not reassignment. actually its modifying value of m using x. this can be verified taking address of m and x as both would be same.
cout<<"x="<<x<<endl;//20
cout<<"m="<<m<<endl;//m gets modified to  20 now

cout<<"&x="<<&x<<endl;//0x7fff9671caf0
cout<<"&m="<<&m<<endl;//0x7fff9671caf0

//reference arithmatic is not allowed
int a=10;
int b=20;
int& ar=a;
cout<<"a="<<a<<endl;//10
cout<<"ar="<<ar<<endl;//10
ar++;//referant gets incremented not reference
cout<<"a="<<a<<endl;//11
cout<<"ar="<<ar<<endl;//11

return 0;
}
/*
i: 10
j: 10
k: 10
&i: 0x7fffee78603c
&j: 0x7fffee78603c
&k: 0x7fffee78603c
x=10
x=20
m=20
&x=0x7fffee786040
&m=0x7fffee786040
a=10
ar=10
a=11
ar=11

*/

C++ reference FAQ interview questions

//1.References are nothing but constant pointer.
//2.So,references once initialized can not refer to other.
//2.1 Reference to reference not allowed.No error but. it will start pointing to the first variable
//3. reference arithmatic not allowed.No error in reference arithmatic but changes reflected in referant not in reference.
//4.Reference to array allowed.//int (&r)[5];
//5.Array of reference not allowed because we can not find the address of array. ultimately refer to first variable.
//6.Unlike pointers,refernce are automatically gets dereferenced.No need to use *(value at address).
//7.Unlike pointers, reference arithmatic like increment will increase the referant's value and not the address
//8.Reference are used to pass by reference to get the changes reflected in calling function.
//9.Pass by reference avoids copy of large value when structure like data are passed as arguments.
//10. Its unsafe to return local variable by value because variable goes out of scope once conrol returns out of the function defintion.
//Solution is us local static variable or dynamic memory allocation so that scope of the variable will through out the program.
#include<iostream>
using namespace std;
int gv;
int main()
{
//reference nothing but constant pointer means once initialized can not be reinitialized.
int s=10;
int *const h=&s;//s is const pointer so can not hold another address
//h=&m;//error: assignment of read-only variable ‘h’


int i=10,m=2;
int &j=i;// j is reference to a

//reference to refernce not allowed. No error but points to the first variable
int &k=j;//reference to reference no error but//point 2.1
k=20;
cout<<i<<endl;//20

//pointer arithmatic are not allowed. no error but changes gets reflected in first variable
k++;//point 7
cout<<i<<endl;//21

//referece can not be reinitialized but direct values can be given
int a=1;
k=1;
cout<<k<<endl;

//pass by reference to get the values reflected in calling function
int x=10,y=20;
swap(x,y);
cout<<x<<" " <<y<<endl;//20,10

//if return type is reference then function call can be made in left side
int &f();
f()=5;
cout<<"gv="<<gv<<endl;//5//instead of returned reference 100, f() is assigned to 5

//problem of local ovariable returning by value
int d;
int ff(d);
cout<<"d="<<d<<endl;//Garbage, because return value went out of scope in definition itself.
return 0;
}
void swap(int x, int y)
{
int t;
t=x;
x=y;
y=x;
}
//return by reference
int &f()
{
gv=100;
return gv;
}
//problem of returning local variable by value
int ff(int t)
{
int& lv=t;
lv=lv+44;
return lv;//returning local variable by value.
}//scope of lv over here
You may also like.