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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago