this pointer in C++

C++ uses a unique keyword called this to represent an object that invokes a member function. this pointer in C++ is a pointer that points to the object for which this function was called.

For example, the function call:

A.max()

will set the pointer this to the address of the object A.

The starting address is the same as the address of the first variable in the class structure.

This unique pointer is automatically passed to a member function when it is called. The pointer this acts as an implicit argument to all the member functions.

Consider the following simple example:

class ABC
{
   int a;
   ...
   ...
};

The private variable a can be used directly inside a member function, like:

a = 123;

We can also use the following statement to do the same job:

this -> a = 123;

Since C++ permits the use of shorthand form a = 123, we have not been using the pointer this explicitly so far.

You may also like...

1 Response

  1. bob says:

    Great great site!
    I can’t wait to read more.
    Congratulations again, from France.

Leave a Reply

Your email address will not be published. Required fields are marked *