The this Pointer

this pointer
this pointer
Basic Concept:

  By default, the compiler provides each member function of a class with an implicit parameter that points to the object through which the member function is called. The implicit parameter is called this. A constant member function is one that does not modify the object through which it is called.


Explanation:

Consider the class
class Example
{
int x;
public:
Example(int a)
{
 x = a;
}
void setValue(int);
int getValue();
};

with the member function

int Example::getValue()
{
return x;
}

that simply returns the value in an object of the class. As an example, the getValue function might be invoked in a program such as

int main()
{
Example ob1(10), ob2(20);
cout << ob1.getValue() << " " << ob2.getValue();
return 0;
}
in which case the program would print out the values 10 20.
You learned in an earlier chapter that the different objects of a structure or class type are called instances of that class, and that each instance of a class has its own copy of the data members listed in the class. These data members, called instance members because they belong to instances of the class, can have different values in different objects. Thus in the preceding example, the instance member x in the ob1 object has a value of 10 while x in ob2 has a value of 20.

Now consider again the code for the member function

int Example::getValue()
{
return x;
}
This function is supposed to return the x member of some object of the Example class, but how does it know which object to use? What happens is that by default, the compiler provides each member function of every class with an implicit parameter that is a pointer to an object of the class. Thus for example, the getValue function is equipped with a single parameter of type pointer to Example. Similarly, the member function

void Example::setValue(int a)
{
x = a;
}

although written by the programmer to take a single parameter of type int, in reality has two parameters: an pointer to an object of the class Example, and the int a parameter specified by the programmer. In all cases, the actual parameter for the implicit object parameter is the address of the object through which the member function is being called. Thus in the call 

ob1.getValue()
the implicit parameter passed to getValue is the address of ob1, whereas in the call
ob2.setValue(78)
the implicit parameter passed to setValue is &ob2.
The implicit pointer passed by the compiler to a member function can be accessed by code inside that function by using the reserved keyword this. So for example, a member function of the Example class could access the object through which it is called by using the expression
*this
and it could also access any of the members of that object through the same pointer. 

Example Program : 

#include <iostream>
using namespace std;

class MyClass {
        int data;
    public:
        MyClass()
        {
            data=100;
            };
        void Print1();
        void Print2();
};

// Not using this pointer
void MyClass::Print1() {
    cout << data << endl;
}

// Using this pointer
void MyClass::Print2() {
    cout << "My address = " << this << endl;
    cout << this->data << endl;
}


int main()
{
    MyClass a;
    a.Print1();
    a.Print2();

    // Size of doesn't include this pointer
    cout << "100 is value of data and the value is memory location"<<endl;
}



Newest
Previous
Next Post »