Categories: C++

Class Members Pointers in C++

A pointer to a C++ class is created in the same way that a pointer to a structure is created, and to access members of a class pointer, use the member access operator ->. To better understand the concept of a pointer to a class, consider the following example:

Example:
#include <iostream>
using namespace std;

class Coderz
{
    public:
    int a;
};

int main()
{
    Coderz obj;
    Coderz* ptr;   // Pointer of class type
    ptr = &obj;
 
    cout << obj.a;
    cout << ptr->a;  // Accessing member with pointer
}
Output:
2189521895
Pointer to Data Members of Class:

The ::* dereferencing operator allows us to create a pointer to a class member, which could be a data member or a member function.

Syntax:

 data-type  class-name ::* pointer-name = &class-name ::  data-member-name;

where,

  • The data-type is the data member’s data type.
  • The class-name is the name of the class that the data member belongs to.
  • The pointer-name is the pointer’s name that points to the member function.
  • The data-member-name specifies the name of the data member that is being referred to.
Pointers with Objects:

To access a class member, the .* dereferencing operator uses a pointer to a class member and an object of a class. This class member could be a data member or a function member.

Object.*pointerToMember

with pointer to object, it can be accessed by writing,

ObjectPointer->*pointerToMember
Example:
#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";
}
Output:
The value of x is : 30
The value ot y is : 30
Pointer to Member Functions:

Pointers can be used to point to the Member functions of a class.

Syntax:

return_type (class_name::*ptr_name) (argument_type) = &class_name::function_name;
Example:
#include<iostream>
using namespace std;
class Coderz
{ 
    public:
    int f(float) 
    { 
        return 1; 
    }
};

int (Coderz::*fp1) (float) = &Coderz::f;   // Declaration and assignment
int (Coderz::*fp2) (float);        // Only Declaration

int main()
{
    fp2 = &Coderz::f;   // Assignment inside main()
}

Note: also read about Copy Constructor in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 day ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago