answersLogoWhite

0

What is role of this pointer?

Updated: 8/11/2023
User Avatar

Wiki User

14y ago

Best Answer

The this pointer is an implicit, compiler generated pointer to the current object that a method is manipulating. It is used to differentiate scope between parameters and members, in the case where they have the same name. Example...

class myclass {
...
int data1;
...
mymethod(int data1) {
data1 = data1; /*ambiguous */
this->data1 = data1; /* not ambiguous */
}
...
secondmethod(int data2) {
data1 = data2; /* not ambiguous */
}
...
}

Many coders use some prefix, such as underscore, to mark member variables. This works, but is not necessary.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

The 'this' pointer is a special pointer that exists in every instance of a class and essentially points to the object itself.

It's quite common for an object to refer to itself by its 'this' pointer. In actual fact it does it all the time without us knowing. Every time we invoke an object's methods from within the object itself, the 'this' pointer's pointsto operator is invoked Behind the Scenes (this->).

A classic example for the this pointer is when overriding the object's assignment operator, both to ensure the incoming object is not a self-reference and also to return a reference to the object itself (which it must do when the assignment is complete).

For example:

class CMyBuffer

{

public:

CMyBuffer():m_pData( NULL ),m_uSize(0){} ~CMyBuffer(){if(m_pData)delete(m_pData);}

CMyBuffer & operator=(const CMyBuffer & rhs);

// remainder of public interface omitted for brevity...

private:

char * m_pData; // Pointer.

unsigned int m_uSize; // Size of memory allocated to pointer.

};

// Assignment operator.

CMyBuffer & CMyBuffer::CMyBuffer(const CMyBuffer & rhs)

{

// Self-reference check.

if( &rhs != this )

{

// Release the current allocation (if any). if( this->m_pData )delete( this->m_pData );m_pData = NULL;

// Copy the size member.if( this->m_uSize = rhs.m_uSize ){// Deep-copy memory.this->m_pData = ( char * ) malloc( this->m_uSize );memcpy( this->m_pData, rhs.m_pData, this->m_uSize );}

}

// Return a reference to this object.

return( *this );

}

Note the explicit use of the this-> operator throughout the assignment method. You wouldn't normally do this because it is implied whenever you access an object's own members. But it helps to clarify exactly which object's members you're accessing, especially when dealing with multiple objects of the same class type.

The important thing to notice is the line that reads: if( &rhs != this ). This is a self-reference check. It is vital that we perform a self-reference check prior to copying the members of another object when one or more of those members are pointers to allocated memory.

If the memory were allocated outside of the object, this would not be such a problem. The memory doesn't belong to the object, so a shallow copy of the pointer is sufficient. But that is not the case in this example. We must deep-copy the memory, which means we must release whatever memory is currently allocated to the pointer.

If we don't perform the check and rhs turns out to be a self-reference, then as soon as we release this object's memory we corrupt the data in rhs -- because they are in fact the same object.* The object has committed a cardinal sin -- it has destroyed the very data it had intended to copy, and there is now way back. Prevention is clearly better than cure in this case.

*Note that it doesn't matter that rhs is declared constant -- we didn't actually access the object using rhs, we used the this pointer, which has to be non-constant in this case (we wouldn't be able to assign otherwise). In any event, the use of constants is simply a convenience for the compiler. At runtime, there are no constants.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is role of this pointer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Role of instruction pointer in 8086?

Its role is to point to the next instruction to be executed in the CPU. It always points to the next instruction to be executed in the CPU


Difference between pointer to constant and constant pointer?

1. pointer to a constant means you can not change what the pointer points to 2. constant pointer means you can not change the pointer.


What is triple pointer?

Example: int x; -- integer int *px= &x; -- pointer to integer int **ppx= &px; -- pointer to pointer to integer int ***pppx= &ppx; -- pointer to pointer to pointer to integer


What are pointer to pointer?

A pointer only holds an address information (location) in the memory. if a pointer holds points another pointer then it is a pointer to an other pointer. Pointer holds an address in the memory so in that address there is an other location information that shows another location.


What is pointer of pointer?

pointer is the variable that holds the address of another variable


Why is double pointer used?

Double pointer is a pointer to a pointer. So you can work with the double pointer as you work with a single one.Or you might mean 'pointer to double', eg:void clear_double (double *dp){*dp = 0;}


Define pointer to pointer in c?

Double (**) is used to denote the double pointer. As we know the pointer stores the address of variable, Double pointer stores the address of any pointer variable. Declaration : int **ptr2Ptr;


What is void pointer and what are its uses?

Void pointer can hold the address of any kind of pointer. But we can't operate on void pointer


C program pointers to pointers examples?

Pointer to Pointer is a double pointer, denoted by (**). Pointer stores the address of the variable and pointer to pointer stores the address of a pointer variable and syntax can be given as int **ptr2ptr;


What is the mouse pointer?

the move pointer


What a cell pointer?

A cell pointer is a


Is a pointer and an English pointer the same?

Pointers come in different breeds. For example: German Short Haired Pointer and English Pointer.