### The this Pointer

The keyword this identifies a special type of pointer. When a nonstatic member function is called, the this pointer identifies the class object which the member function is operating on. You cannot declare the this pointer or make assignments to it.

The type of the this pointer for a member function of a class type X, is X* const. If the member function is declared with the constant qualifier, the type of the this pointer for that member function for class X, is const X* const. If the member function is declared with the volatile qualifier, the type of the this pointer for that member function for class X is volatile X* const.

this 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.

For example, you can refer to the particular class object that a member function is called for by using the this pointer in the body of the member function. The following code example produces the output a = 5:

    // This example illustrates the this pointer

    #include <iostream.h>
    class X
    {
          int a;
    public:
          // The 'this' pointer is used to retrieve 'xobj.a' hidden by
          // the automatic variable 'a'
          void Set_a(int a) { this->a = a; }
          void Print_a() { cout << "a = " << a << endl; }
    };
    void main()
    {
          X xobj;
          int a = 5;
          xobj.Set_a(a);
          xobj.Print_a();
    }

Unless a class member name is hidden, using the class member name is equivalent to using the class member name qualified with the this pointer.

Example of the this Pointer

Pointers
Member Functions
Pointers to Members