Member Dereferencing Operator

In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable.

It returns the location value, or l-value in memory pointed to by the variable’s value. In the C programming language, the dereference operator is denoted with an asterisk ( * ).

For example, in C, we can declare a variable M that holds an integer value, and a variable *ptr that holds a pointer to an integer value in memory:

#include<iostream>
using namespace std;

	void main()
	{
		int M;
		int *ptr;
		M = 5;
		ptr= &M;
		cout << *ptr_p;
	}

In C++ Programming we have three different dereferencing operators.

  1. Pointer to a member declarator ( ::* )
  2. Pointer to member operator( .* )
  3. Pointer to member operator ( ->* )

1. Pointer to a member declarator ( ::* )

This operator is used for declaring a pointer to the member of the class. It is possible to take the address of a member of a class and assign it to a pointer. The address of a member can be obtained by applying the operator & to a “fully qualified” class member name.

A class member pointer can be declared using the operator :: * with the class name. We can define a pointer to the member m of class ABC as follows :
Syntax:

int ABC :: * ip = & ABC :: m
#include<iostream.h>
class ABC
{
     public:
           int m;
};
int main()
{ 
    ABC A;                      //object
    int ABC ::*ip = & ABC :: m;  //pointer decleration
    *ip=20;
    cout<<ABC::m;
}

Output:20

The ip pointer created thus acts like a class member in that it must be invoked with a class object. In the above statement. The phrase ABC :: * means “pointer – to – member of a class” . The phrase & ABC :: m means the “ Address of the m member of a class”

The following statement is not valid :

int *ip=&m; // invalid

This is because m is not simply an int type data. It has meaning only when it is associated with the class to which it belongs. The scope operator must be applied to both the pointer and the member. The pointer ip can now be used to access the m inside the member function (or friend function).

2. Pointer to member operator( .* )

Let us assume that “A” is an object of “ABC” declared in a main function . We can access “m” using the pointer ip as follows.

#include<iostream.h>
class ABC
{
     public:
 
         int x;
};
int main()
{ 
    ABC A;         //object
    int ABC::*ip;  //pointer declaration
    ip=&m ;        //pointer initialization
    A.*ip=10;      //value initialization
    cout<<A.*ip;
}
Output:10

In the above statement. The phrase ABC :: * means “pointer – to – member of a class” . The phrase A .* ip=10 means “the initialization of member with value ”

3.Pointer to member operator ( ->* )

Let us assume that “A” is an object of “ABC” declared in a main function . We can access “m” using the pointer ip as follows.

#include<iostream.h>
class ABC
{
   public:
       int m;
       void display()
       {
          cout<<"x="<<x<<endl;
       }
};
int main()
{
     ABC A;
     ABC *ptr;
     int ABC::*f=&ABC::m;
     A.m=10;
     ptr=&A;
     cout<<ptr->*f;
     ptr->display();
}

In the above code we have created a pointer *ptr for the object A using statement “ptr=&A;”. so now we can make use of *ptr instead of using object of class to access members. The statement int ABC::*f=&ABC::m; is creating pointer *f to member variable m. Now at last we are accessing member variable m using the pointer on both the side with statement “cout<<ptr->*f;”

Related posts