In our last article, we have explained how to use the dereferencing operators to access the data members of a class.
Moving on, in this article we are going to explain how to use the dereferencing operators to access the member functions of a class.
For those who don't know anything the dereferencing operators and have not read our last article about how to access data members of class(which you should), let's give you a quick summary about
the dereferencing operators in C++.
Dereferencing operators in C++
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.
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 member functions
Let us see how to us dereferencing operators to access the member functions 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 return-type is the return type of the member function.
The class-name is the class name of the class of which the member function is a part of.
The pointer-name is the name of the pointer, pointing to the member function.
The member-function-name is the name of the member function being referenced.
The .* dereferencing operator uses the 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 pointer-to-object is the name of the pointer, pointing to the object of class.
The pointer-to-member-function is the name of the pointer, pointing to the member function.
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;
void put_value(int a, int b)
{
x=a;
y=b;
}
void show_value()
{
cout<<"The value of x is : " << x << "\n";
cout<<"The value ot y is : " << y << "\n";
}
};
int main()
{
//A Pointer to member function
void (A :: *pt1_f) (int,int) = &A :: put_value;
//A pointer to member function
void (A :: *pt2_f) () = &A :: show_value;
//Creating an object of class A
A ob;
//Using a pointer-to-member-function to call the member function put_value()
(ob.*pt1_f)(20,20);
//Using a pointer-to-member-function to call the member function show_value()
(ob.*pt2_f)();
}
Output
The value of x is : 20
The value ot y is : 20
Advertisement
Dereferencing operators ::* and ->* to access the member functions
Let us see how to us dereferencing operators to access the member function 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 return-type is the return type of the member function.
The class-name is the class name of the class of which the member function is a part of.
The pointer-name is the name of the pointer, pointing to the member function.
The member-function-name is the name of the member function being referenced.
The ->* This operator uses a 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 class member could be a data member
or a member function.
The syntax of ->* dereferencing operator to call a member function-
The pointer-to-object is the name of the pointer, pointing to the object of class.
The pointer-to-member-function is the name of the pointer, pointing to the member function.
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;
void put_value(int a, int b)
{
x=a;
y=b;
}
void show_value()
{
cout<<"The value of x is : " << x << "\n";
cout<<"The value ot y is : " << y << "\n";
}
};
int main()
{
A ob;
//Pointer to object
A *ptr = &ob;
int A :: *p1 = &A :: x;
int A :: *p2 = &A :: y;
//A Pointer to member function
void (A :: *pt1_f) (int,int) = &A :: put_value;
//A pointer to member function
void (A :: *pt2_f) () = &A :: show_value;
//Using pointer to object to access member function, pointed by a pointer
(ptr->*pt1_f)(40,40);
//Using pointer to object to access member function, pointed by a pointer
(ptr->*pt2_f)();
}