Reference Variables in C++

  • This concept is used in C++.
  • When we create a variable of any data type, we use single identifier to identify that variable.

Example:    int i;

  • In this case, i is an identifier from which we can access that variable.
  • If we want to assign some another name to that same identifier then we can use the reference variable concept from C++.
  • A reference variable are such a variable which are refering to already created datatypes.
  • A reference variables are the names given to the variable.
  • Entry of that another name is in the symbol table.
  • We can create a reference variable for any data type.
  • As said above reference variables are only another name, due to which memory is not allocated for a reference variable.
  • Reference variables are needs to be initialized at the time of declaration.
  • We cannot create array of a reference variables.
  • Multiple references to the single variable are allowed.
  • But same reference variable to multiple variables is not allowed.
  • A reference to a reference variable is allowed.
  • We cannot create array of a references, because a reference variable does not get any memory.
  • But we can create a reference to an array.

//

Example:.

int main()

{

int i=10;

int &f=i;

printf(“%d”,i);

printf(“%d”,f);

return 0;

}

//

int main()

{

int i=10;

int &f=i;

int &d=f;

int &s=d;

//

// Reference to reference variable

//

printf(“%d”,i); //10

printf(“%d”,f); //10

printf(“%d”,d); //10

printf(“%d”,s); //10

return 0;

}

//

int main()

{

int i=10;

int &f=i;

int &d=i;

int &s=i;

//

// Multiple reference to single a variable

//

printf(“%d”,i); //10

printf(“%d”,f); //10

printf(“%d”,d); //10

printf(“%d”,s); //10

return 0;

}

C++ Reference Variables

//

int main()

{

// Error

// One reference can not refer to different variables

//

int i=10;

int k=20;

int &f=i;

int &f=k;

return 0;

}

//

//

// We can create a reference variable as a member of a class

// Memory is allocated for it also iside the class

// It must be initialized using the initialization list

//

class demo

{

public:

int i;

int &j;

demo():j(i)

{

i=10;

}

};

int main()

{

demo d;

cout<<d.i;//10

d.j++;

cout<<d.i; //11

cout<<d.j; //11

cout<<sizeof(d); // 8

return 0;

}

//

int main()

{

//

// We can create a reference to an array

//

int a[4]={1,2,3,4};

int (&d)[4] = a;

cout<<a[0]; // 1

cout<<a[1]; // 2

cout<<a[2]; // 3

cout<<a[3]; // 4

cout<<d[0]; // 1

cout<<d[1]; // 2

cout<<d[2]; // 3

cout<<d[3]; // 4

return 0;

}

//

int main()

{

int i=90;

int k=90;

int l=45;

int &f=i;

int &d=k;

int &a=l;

// Error

// We can not create an array of a references

//

int &(c[4]) = {f,d,a};

return 0;

}

//

int main()

{

int i=90;

int k=80;

int l=45;

int &f=i;

int &d=k;

int &a=l;

//

// In this way we can create an array of a references

//

int (c[4]) = {f,d,a};

printf(“%d”,c[0]); // 90

printf(“%d”,c[1]); // 80

printf(“%d”,c[2]); // 45

return 0;

}

//

For more reading about technology news in singapore and seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply