In the C + + programming language, a reference is a simple reference datatype that's less powerful but safer than the point ER type inherited from C. The name C + + reference may cause confusion, as in computer science a reference are a general concept datatype with pointer s and C + + references being specific reference datatype implementations. The definition of a reference in C + + is such this it does not need to exist. It can be implemented as a new name for the existing object (similar to rename keyword in Ada).
C + + references differ from pointers in several essential ways:
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 being later made to reference another object; It cannot be reseated. This is often do with pointers.
References cannot be null, whereas pointers can; Every reference refers to some object, although it could or may is not valid. Note that for this reason, containers of references is not allowed.
References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must being initialized as soon as they is created. In particular, local and global variables must is initialized where they is defined, and references which is data member S of class instances must is initialized in the initializer list of the class ' s constructor.
References can thought of as "symbolic links" in the file system terminology. Symbolic links can modified as though they were the file they were linked to and if a symbolic link is deleted the OR Iginal file remains. Similarly, references can be deleted (whether by going out of the scope or explicitly being removed if they were allocated in Heap) and the original object that is referenced remains. Similarly, once a symbolic link have been created it can never be changed.
#include <iostream> #include <stdlib.h>using namespace std;void fun (int &a, int &b); int main (void) { int x = 10;int y = 20;cout << x << "," << y << endl;fun (x, y); cout << x << "," << ; Y << endl;system ("pause"); return 0;} void Fun (int &a, int &b) {int c = 0;c = A;a = B;b = C;}
The program uses references to achieve the interchange of data.
C + + Reference