Tuesday, February 15, 2011

“this” pointer:


“this” pointer:
In addition to the explicit parameters in their argument lists, every class member function (method) receives an additional hidden parameter, the "this" pointer. The "this" pointer addresses the object on which the method was called. Thus, this pointer is used as a pointer to the class object instance by the member function.
                  Each object maintains its own set of data members, but all objects of a class share a single set of methods. This is, a single copy of each method exists within the machine code that is the output of compilation and linking. A natural question is then if only a single copy of each method exists, and its used by multiple objects, how are the proper data members accessed and updated. The compiler uses the "this" pointer to internally reference the data members of a particular object. Suppose, we have an Employee class that contains a salary member and a setSalary method to update it. Now, suppose that two Employees are instantiated.
class Employee
 {
   public:
     void setSalary(double sal)
     { salary = sal; }
    
void display()
            { cout<<"\nThe salary:"<<salary;  }
   private:
      double salary;
 };

int main()
{   Employee programmer;
    Employee managar;
    managar.setSalary(60000.0);
    programmer.setSalary(40000.0);
    managar.display();
    programmer.display();
    }
                      If only one setSalary method exists within the binary that is running, how is the correct Employee's salary updated? The compiler uses the "this" pointer to correctly identify the object and its members. During compilation, the compiler inserts code for the "this" pointer into the function. The setSalary method that actually runs is similar to the following pseudo-code.
void setSalary(Employee *this, float sal)
{
    this->salary = sal;
}
The correct object is identified via the "this" pointer.
Example: This example show the use of this for displaying starting address of object

class this_example
  {
   private:
       int a;
       float data;
   public:
      void address()               // display address of the object which calls the function
       {  cout<<"\nThe address of object is "<<this;   }
  };
  void main()
    {
    this_example obj1, obj2;
    obj1.address();
    obj2.address();
    }


Output:
The address of object is 0x8fd3fff0
The address of object is 0x8fd3ffea

Example: This example shows the use this pointer
#include<iostream.h>
#include<conio.h>
 class this_pointer_example
      {
        int data1;
      public:
         int getdata()   //Function using this pointer for C++ Tutorial
              {   return this->data1;   }
         void setdata(int newval)            //Function without using this pointer
              {  this->data1 = newval;    }  //same as data1=newval;
        };

void main()
  {
   int result;
   this_pointer_example obj;
   obj.setdata(10);
   result=obj.getdata();
  cout<<"\nThe result is: "<<result;;  }

Output:
 The result is 10

   Characteristics of this pointer:
  • this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
  • this pointers are not accessible for static member functions.
  • this pointers are not modifiable.  

No comments:

Post a Comment