C++ allows us to create a class with its members such as data members and member functions.
One way to access the data members and member function of a class is through the object of the class with a dot operator.
C++ also provides us a few operators through we could access the data members and member functions of a class by using pointers.
These operators are known as dereferencing operators.
In this article, we are going to use these dereferencing operators to access the data members of a class.
Let's see a little description of these dereferencing operators in C++.
Derefencing Operators
Description
::*
This operator allows us to create a pointer to a class member, which could be data member or member function.
->*
This operator uses the pointer to the member of a class and a pointer to the object of the same class, to access the member of a class.
.*
This operator uses the pointer to the member of a class and an object of the same class, to access member of a class.
Note
The dereferencing operators like .* or ->* and used in a combination with another dereferencing operator ::* to access the members of a class
such as data members or member functions.
Dereferencing operators ::* and .* to access the data members
Let us see how to use the dereferencing operators to access the data members of a class.
For this, we are going to use dereferencing operators such ::* and .*
The ::* dereferencing operator allows us to create a pointer to a class member, which could be a data member
or a member function.
The data-type is the data type of the data member.
The class-name is the class name of the class of which the data member is a part of.
The pointer-name is the name of the pointer, pointing to the member function.
The data-member-name is the name of the data member being referenced.
The .* dereferencing operator uses a pointer to a class member and an object of a class,
to access the member of a class. This class member could be a data member
or a member function.
The syntax of .* dereferencing operator is -
object-name.*pointer-to-data-member;
The pointer-to-object is the name of the pointer, pointing to the object of class.
The pointer-to-data-member is the name of the pointer, pointing to the data member.
The argument-list is the list of arguments passed to a function or an empty pair of parentheses(), if the function is not to be passed any arguments.
#include<iostream>
using namespace std;
class A
{
public:
int x,y;
};
int main()
{
//Pointer to member variable x of class A
int A::* p1 = &A :: x;
//Pointer to member variable y of class A
int A::* p2 = &A :: y;
//Creating an object of class A
A ob;
ob.*p1 = 10;
ob.*p2 = 10;
cout<<"The value of x is : " << ob.*p1 << "\n";
cout<<"The value ot y is : " << ob.*p2 << "\n";
}
Output
The value of x is : 20
The value ot y is : 20
Advertisement
Dereferencing operators ::* and ->* to access the data members
Let us see how to us dereferencing operators to access the data members of a class.
For this, we are going to use dereferencing operators such ::* and ->*
The ::* dereferencing operator allows us to create a pointer to a class member, which could be a data member
or a member function.
The data-type is the data type of the data member.
The class-name is the class name of the class of which the data member is a part of.
The pointer-name is the name of the pointer, pointing to the member function.
The data-member-name is the name of the data member being referenced.
The ->* This operator uses a pointer to the class member and a pointer to the object of the same class, to access the member of a class.
This class member could be a data member
or a member function.
The syntax of ->* dereferencing operator is -
pointer-to-object->*pointer-to-data-member;
The pointer-to-object is the name of the pointer, pointing to the object of class.
The pointer-to-data-member is the name of the pointer, pointing to the data member.
The argument-list is the list of arguments passed to a function or an empty pair of parentheses(), if the function is not to be passed any arguments.
#include<iostream>
using namespace std;
class A
{
public:
int x,y;
};
int main()
{
A ob;
//Pointer to object
A *ptr = &ob;
int A :: *p1 = &A :: x;
int A :: *p2 = &A :: y;
//Using pointer to object to access data member, pointed by a pointer
ptr->*p1 = 30;
//Using pointer to object to access data member, pointed by a pointer
ptr->*p2 = 30;
cout<<"The value of x is : " << ptr->*p1 << "\n";
cout<<"The value ot y is : " << ptr->*p2 << "\n";
}