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

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago