Open In App

Why do we need reference variables if we have pointers

Pointers: A pointer is a variable that holds memory address of another variable. A pointer needs to be de referenced with * operator to access the memory location it points to.
References: A Reference can be called as a constant pointer that becomes de referenced implicitly. When we access the reference it means we are accessing the storage.
Why do we need reference variables if we have pointers?
In Pointers to access the value of the actual variable, we need to explicitly dereference the pointer variable by using ‘value at address’ dereferencing operator(*).
In References to access the value of the actual variable, we do not need to explicitly dereference the reference variable, they get de-referenced automatically.
Pointers and References are equivalent, except: 

For Example: If a is a pointer to integer type, *a returns the value pointed to by a
To assign an address of a variable b into a pointer, we need to use the address-of operator(&).
For Example: int *a= &b.
 



Object slicing happens when a derived class object is assigned to a base class object, additional attributes of a derived class object are sliced off to form the base class object.

Below is the program for the illustration of pointer and references: 






// C++ program to illustrate
// pointer and references
 
#include <iostream>
using namespace std;
 
// function to illustrate
// pointer and references
void pointer_vs_reference()
{
    int num1 = 20, num2 = 23;
 
    // Pointer pointing to num1
    // Explicit referencing
    int* ptr1 = &num1;
    cout << *ptr1 << endl; // 20
 
    // Explicit dereferencing
    *ptr1 = 26;
    cout << *ptr1 << endl; // 26
 
    // pointer can be reassigned to
    // store another address
    ptr1 = &num2;
    cout << *ptr1 << endl; // 23
 
    // Reference to num1
    // Implicit referencing
    int& ref1 = num1;
    cout << ref1 << endl; // 26 not 20
 
    // Implicit dereferencing
    ref1 = 18;
 
    cout << ref1 << endl; // 18
 
    // reference cannot be reassigned
}
 
// Driver code
int main()
{
    pointer_vs_reference();
    return 0;
}

Output: 
20
26
23
26
18

 

Explanation of output: 

Illustration of Reference Variable:
Best example of the reference variable is the concept of copy constructor. Copy constructor takes a reference variable as an argument, pointer cannot be used here.




#include <iostream>
using namespace std;
 
class complex {
private:
    int a, b;
 
public:
    // Parametric constructor
    complex(int x, int y)
    {
        a = x;
        b = y;
    }
    // Copy constructor
    complex(const complex& c)
    {
        a = c.a;
        b = c.b;
    }
 
    // Function to print data
    void printData()
    {
        cout << "a = " << a << endl;
        cout << "b = " << b;
    }
};
 
int main()
{
 
    complex c1(5, 10);
    complex c2(c1);
    c2.printData();
}

Output: 
a = 5
b = 10

 

Explanation: In the above example if we take pointer in the argument of copy constructor then object of complex class will be created again and again which will never be stopped and it is error in oops concept. choosing reference is only the solution in this condition.


Article Tags :