Home » C++ programming language

C++ Reference variable

C++ introduces a new kind of variable known as Reference Variable. It provides an alias (alternative name) for a previously defined variable.

A reference variable must be initialized at the time of declaration.

This establishes the correspondences between the reference and the data onject which it name.

When a reference is created, you must tell it which variable it will become an alias for. After you create the reference, whenever you use the variable, you can just treat it as though it were a regular integer variable. But when you create it, you must initialize it with another variable, whose address it will keep around behind the scenes to allow you to use it to modify that variable.


    [data_type] & [reference_variable]=[regular_variable];

regular_variable is a variable that has already initialized, and reference_variable is an alternative name (alias) to represent the variable regular_variable.

Consider the example

#include <iostream.h>
int main()
	int student_age=10;
	int &age=student_age;	// reference variable

	cout<< " value of student_age :"<< student_age << endl;
	cout<< " value of age :"<< age << endl;

	cout<< " value of student_age :"<< student_age << endl;
	cout<< " value of age :"<< age << endl;
	return 0;


    value of student_age :10
    value of age :10

    value of student_age :20
    value of age :20

Reference Variables as Function Parameter (Call by reference)

A Reference Variable can also be used in function parameter, this is known as call by reference.

The call by reference mechanism is useful in object oriented programming due to following reasons:

  • It permits the manipulation of objects by reference.
  • It eliminates the copying of object parameters.
  • Reference variables can be created for user defined data types such as structures and classes.

Consider the example

//Program to illustrate call by references.
#include <iostream.h>

// function call by reference
void fun(int &x)

int main()
	int m=100;
	cout<<"\n Value of m is : "<< m << endl;
	return 0;


    Value of m is : 110

Difference between Pointer and Reference

  • It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.
  • Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
  • References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.
  • References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.