×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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.

Declaration of Reference Variable

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.

Declaration Syntax

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

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

Example of Reference variable

Consider the example

#include <iostream>
using namespace std;

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;

  age = age + 10;
  cout << "\nAFTER ADDING 10 INTO REFERENCE VARIABLE \n";
  cout << " value of student_age :" << student_age << endl;
  cout << " value of age :" << age << endl;
  return 0;
}

Output

value of student_age :10
value of age :10

AFTER ADDING 10 INTO REFERENCE VARIABLE
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.

Example

Consider the example

// Program to illustrate call by references.
#include <iostream>
using namespace std;

// function call by reference
void fun(int &x) { x += 10; }

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

Output

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.

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.