answersLogoWhite

0

What is the use of 'this' pointer?

User Avatar

Anonymous

12y ago
Updated: 8/20/2019

Every instance of a class inherits a 'this' pointer. It always points to the instance itself. Outside of the object you must use the object's variable name to refer to the object, or instantiate a pointer to the object. But from within the object's member methods you must use the 'this' pointer which is instantiated automatically as soon as the object is constructed and falls from scope when the destructor returns. Only non-static member functions have access to the 'this' pointer.

There are several uses, however the most important is when checking for self-references, particularly in the assignment operator overload. That is, any member function that accepts a reference to the same class of object should always check for self-references before attempting to mutate the instance. This is particularly important when the class "owns" memory that is dynamically allocated to it.

It is also used to return a reference to the current instance from the assignment operator and from any other operator overload or function that must return a reference to the current instance (including the addition and subtraction operators). Both uses can be seen in the following stripped-down example:

class MyObject

{

public:

// Assignment operator overload.MyObject& operator= ( const MyObject & obj ){

// Self-reference check.

if( this != &obj ){// Assignment code goes here...

}

// Return a reference to this object.

return( *this );

}

};

The 'this' pointer can also be used to pass a pointer (this) or reference (*this) to external functions that accept such arguments.

It should also be noted that when referring to an instance member from within a non-static member function, the dereferenced 'this' pointer is implied, as in:

this->[member_name]

Although this usage is never required, there may be times when it can help make your code more readable, or less ambiguous, especially when a member function must handle one or more external instances of the same class.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Why you use an array of pointer to pointer?

because u freakin can


Use of double pointer?

A double pointer has two basic meanings. One is ofa pointer to a pointer, where changing the value ofdouble pointer will result in the original pointer being changed. Another is that of a two-dimentional array, such as a matrix, or a list of char* (e.g. in main when you use argv).


What is pointer why you use pointer?

Pointer is a variable that stores the address of another variable. Since pointer is also akind of variable, thus pointer itself will be stored at a different memory location.


Difference between genric pointer and normal pointer?

Generic pointer of type 'void *' is compatible with any (data-)pointer, but you cannot use the following operators on it: + - ++ -- += -= * -> []


What is the use of near pointer?

*


Why would you use a pointer to pointer a in aprogram?

A pointer to pointer has many uses, one of the simplest being 2D arrays (matrices). Compacting garbage collectors also often employ pointer pointers.


What pointer type will you use to implement a heterogeneous linked list in c?

void pointer


What is the use of null pointer?

Pointer is a variable that stores address of a variable . A NULL Pointer a pointer that doesn't point to anything, it is a literal zero .Some people ,notably C++ programmers, prefer to use 0 rather than NULL.


Why you use float pointer instead of integer pointer?

It depends on what type of data you wish to manipulate.


How does one use a Green Laser Pointer II?

A Green Laser Pointer is very simple to use. In order to see the green laser from the pointer, you press the button and point it onto an opaque surface.


In pointers what is the use of pointer variable?

Pointer is a variable that stores the address of another variable . So pointer basically stores the address of another variable and size of pointer can be evaluated by using sizeof operator.


What are the applications of double pointer in c?

insert or delete values both side.so use double pointer