Topic :Introduction To "This" Pointer | Pointer To Classes Object:

Pointer To Objects:

A Variable That Holds an Address value is called a Pointer variable or simply pointer. We already discussed about pointer that's point to Simple data types likes int, char, float etc. So similar to these type of data type, Objects can also have an address, so there is also a pointer that can point to the address of an Object, This Pointer is Known as This Pointer.



This Pointer:

Every Object in C++ has access to its own address through an important pointer called This Pointer. The This Pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. Whenever a member function is called, it is automatically passed an implicit arguments that is This pointer to the invoking object (i.e. The object on which the function is invoked).



Nonstatic Member Function: If the function is declared in the class without a "Static" or "Friend" Specifier, then this function is known as Nonstatic Member Function.

Static Member Function: If the function is declared in the class using "Static" or "Friend" Specifier, then this function is known as static Member Function.

The This pointer is passed as a hidden argument to all Nonstatic member function calls and is available as a local variable within the body of all Nonstatic functions. This Pointer is a constant pointer that holds the memory address of the current object. This pointer is not available in static member functions as static member functions can be called without any object (with class name).

Let's have an simple example of This pointer.

/* This Pointer: InfoBrother */
 
#include<iostream>
using namespace std;
 
class Point
{
    private:
        int x;
 
    public: 
        void setx(int a) //simple function.
        {
            this->x=a;
        }
};
 
main()
{
   Point obj1;   //1st Object of Class Point.
   Point obj2;   //2nd Object of Class point.
 
   obj1.setx(10); //function calling using 1st object.
   obj2.setx(30); //function calling using 2nd object.
 
   return 0;
}


In Above Example, there is an simple class having two objects, and a simple function that take an integer as an parameter, and initialize it to variable x. Now in above Example, Might be you have a Question, That

How Setx() function understand which object 'x' value to be set?

So the Answer is, When the function is called the address of the calling object is passed implicitly. so in order to understand, consider the following statement.


obj1.setx( 10 );


Here value 10 is passed explicitly to the function setx() but at the same time Address of obj1 is passed implicitly to the function. " Int a" holds the value 10, and the address of obj1 stored in a pointer named " this " pointer.

Similarly when obj2 calls the function of the class, " This " pointer holds address of obj2 to refer obj2's data member. at shown in figure:



image: Pointer to Object

C++ Declaration and Uses of Object Pointers:

Just like Other Pointers, the object pointers are declared by placing in front of a object pointer's name. The Syntax is:


class_name * Object_pointer_name;

In 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 declared an Pointer ptr an an object pointer of class Date, We shall write:


Date * ptr;

In Above syntax, Date is already defined class. When accessing members of a class using an object pointer, the Error Operator -> is used instead of Dot Operator. Let's take an simple example, This Example illustrates how to access an object given a pointer to it. The following Example illustrates the use of object pointer.



/* Pointer to Object: This Pointer: InfoBrother:*/
 
#include<iostream>
using namespace std;
 
class Date
{
    private:
        short int dd, mm, yy;
 
    public:
        Date() //constrctor:
            {
                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";
            }
};
 
main()
{
    Date D1; //simple object having type Data: 
    Date *dptr; //Pointer Object having type Date:
 
    cout<<"Initializing data members using the 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<<"\nInitializing data members using the object pointer, 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.









I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement