PDFVersion
No ads? No problem! Download the PDF book of this tutorial for just $24.99. Your support will help us reach more readers.
Thank You!
POINTERS TO OBJECTS:
A pointer variable, or simply a pointer, holds an address value. Previously, we discussed pointers pointing to simple data types like int, char, float, etc. Similarly, objects can also have addresses, and there exist pointers that can point to the address of an object. This pointer is known as the this
pointer.
In C++, every object has access to its own address through a significant pointer called the this
pointer. The this
pointer is an implicit parameter to all member functions. Hence, inside a member function, this
may be used to refer to the invoking object. Whenever a member function is called, it automatically receives an implicit argument, which is the this
pointer to the invoking object (i.e., the object on which the function is invoked).
The this
pointer is passed as a hidden argument to all non-static member function calls and is available as a local variable within the body of all non-static functions. It's important to note that the this
pointer is a constant pointer holding the memory address of the current object. However, this pointer is not available in static member functions as static member functions can be called without any object (using the class name).
Static Member Function:
If a function is declared in a class using a "static" or "friend" specifier, then this function is known as a static member function.
Non-Static Member Function:
If a function is declared in a class without a "static" or "friend" specifier, then this function is known as a non-static member function.
#include<iostream>
using namespace std;
class Point
{
private:
int x;
public:
void setx(int a)
{
this->x = a;
}
};
int main()
{
Point obj1; //1st object
Point obj2; //2nd object
obj1.setx(10); //Set value using 1st object.
obj2.setx(30); //Set value using 2nd object.
return 0;
}
In the above example, there's a simple class with two objects and a function that takes an integer parameter and initializes it to the variable x. Now, you might wonder:
How does the setx()
function understand which object's 'x' value to set?
When the function is called, the address of the calling object is passed implicitly. Therefore, to understand, consider the following statement:
obj1.setx(10);
Here, the value 10 is passed explicitly to the function setx()
, and simultaneously, the address of obj1 is passed implicitly to the function. Int aholds the value 10, and the address of obj1 is stored in a pointer named this
.
Similarly, when obj2 calls the function of the class, the this
pointer holds the address of obj2 to refer to obj2's data member.
Just like other pointers, object pointers are declared by placing a class name in front of an object pointer's name. The syntax is:
class_name*object_pointer_name;
In the above syntax, class_name is the name of an already defined class, and object_pointer_name is the pointer to an object of this class type. For example, to declare a pointer ptr as an object pointer of class Date, we shall write:
Date*ptr;
When accessing members of a class using an object pointer, the arrow operator ->
is used instead of the dot .
operator. Let's take a simple example. This example illustrates how to access an object given a pointer to it:
#include<iostream>
using namespace std;
class Date
{
private:
short int dd, mm, yy;
public:
Date() //constructor
{
dd = mm = yy = 0;
}
void getdata(int i, int j, int k)
{
dd = i;
mm = j;
yy = k;
}
void prndata(void)
{
cout<<"\nData is "<<dd<<"/"<<mm<<"/"<<yy<<"\n";
}
};
int main()
{
Date D1; //simple object having type Date
Date *dptr; //Pointer Object having type Date
cout<<"Initializing object with values 19, 10, 2016."<<endl;
D1.getdata(19,10,2016);
cout<<"Printing members using the object ";
D1.prndata();
dptr = &D1;
cout<<"Printing members using the object pointer ";
dptr->prndata();
cout<<"\n Initializing object with values 20, 10, 2016"<<endl;
dptr->getdata(20, 10, 2016);
cout<<"Printing members using the object ";
D1.prndata();
cout<<"Printing members using the object pointer ";
dptr->prndata();
return 0;
}
The above example is self-explanatory. To access public members using an object, the dot .
operator is used, and to access public members using an object pointer, the arrow ->
operator is used.
Sardar Omar
I did my hardest to present you with all of the information you need on this subject in a simple and understandable manner. However, if you have any difficulties understanding this concept or have any questions, please do not hesitate to ask. I'll try my best to meet your requirements.