The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
this this->member-identifier
An object's this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function. For example, the following function call:
myDate.setMonth( 3 );
can be interpreted this way:
setMonth( &myDate, 3 );
The object's address is available from within the member function as the this pointer. Most uses of this are implicit. It is legal, though unnecessary, to explicitly use this when referring to members of the class. For example:
void Date::setMonth( int mn )
{
month = mn; // These three statements
this->month = mn; // are equivalent
(*this).month = mn;
}
The expression *this is commonly used to return the current object from a member function:
return *this;
The this pointer is also used to guard against self-reference:
if (&Object != this) {
// do not execute in cases of self-reference
Note
|
|---|
|
Because the this pointer is nonmodifiable, assignments to this are not allowed. Earlier implementations of C++ allowed assignments to this. |
Occasionally, the this pointer is used directly — for example, to manipulate self-referential data structures, where the address of the current object is required.
// this_pointer.cpp
// compile with: /EHsc
#include <iostream>
#include <string.h>
using namespace std;
class Buf
{
public:
Buf( char* szBuffer, size_t sizeOfBuffer );
Buf& operator=( const Buf & );
void Display() { cout << buffer << endl; }
private:
char* buffer;
size_t sizeOfBuffer;
};
Buf::Buf( char* szBuffer, size_t sizeOfBuffer )
{
sizeOfBuffer++; // account for a NULL terminator
buffer = new char[ sizeOfBuffer ];
if (buffer)
{
strcpy_s( buffer, sizeOfBuffer, szBuffer );
sizeOfBuffer = sizeOfBuffer;
}
}
Buf& Buf::operator=( const Buf &otherbuf )
{
if( &otherbuf != this )
{
if (buffer)
delete [] buffer;
sizeOfBuffer = strlen( otherbuf.buffer ) + 1;
buffer = new char[sizeOfBuffer];
strcpy_s( buffer, sizeOfBuffer, otherbuf.buffer );
}
return *this;
}
int main()
{
Buf myBuf( "my buffer", 10 );
Buf yourBuf( "your buffer", 12 );
// Display 'my buffer'
myBuf.Display();
// assignment opperator
myBuf = yourBuf;
// Display 'your buffer'
myBuf.Display();
}
my buffer your buffer
Reference
The example has the statement:
sizeOfBuffer = sizeOfBuffer;
It clearly will not work as intended.
this->sizeOfBuffer = sizeOfBuffer;
// this_pointer.cpp
// compile with: /EHsc
#include <iostream>
#include <string.h>
using namespace std;
class Buf
{
public:
Buf( const char* szBuffer, size_t sizeOfBuffer ); // FIXED: const char*
Buf& operator=( const Buf & );
void Display() const { cout << buffer << endl; } // FIXED: const method
~Buf( ); // FIXED: destructor to clean up dynamic memory
private:
char* buffer;
size_t sizeOfBuffer;
Buf( const Buf & ); // FIXED: disabled copy constructor
};
Buf::Buf( const char* szBuffer, size_t sizeOfBuffer )
{
//sizeOfBuffer++; // account for a NULL terminator // FIXED: argument accounts for NULL
buffer = new char[ sizeOfBuffer ];
if (buffer)
{
strcpy_s( buffer, sizeOfBuffer, szBuffer );
this->sizeOfBuffer = sizeOfBuffer; // FIXED: must use 'this' to access hidden data member!!!
}
else // FIXED: handle out-of-memory setup
this->sizeOfBuffer = 0;
}
Buf& Buf::operator=( const Buf &otherbuf )
{
if( &otherbuf != this )
{
//if (buffer) // FIXED: 'delete' already checks for null pointer
delete [] buffer;
//sizeOfBuffer = strlen( otherbuf.buffer ) + 1; // FIXED: assignment operator doesn't need to calculate
//buffer = new char[sizeOfBuffer];
buffer = new char[ otherbuf.sizeOfBuffer ];
if (buffer) // FIXED: check that memory was allocated
{
sizeOfBuffer = otherbuf.sizeOfBuffer; // FIXED: assignment operator should keep otherbuf size
strcpy_s( buffer, sizeOfBuffer, otherbuf.buffer );
}
else // FIXED: handle out-of-memory assignment
sizeOfBuffer = 0;
}
return *this;
}
Buf::~Buf( ) // FIXED: destructor required for dynamic data member
{
delete [] buffer;
buffer = 0;
sizeOfBuffer = 0;
}
int main()
{
Buf myBuf( "my buffer", 10 );
Buf yourBuf( "your buffer", 12 );
// Display 'my buffer'
myBuf.Display();
// assignment opperator
myBuf = yourBuf;
// Display 'your buffer'
myBuf.Display();
return 0; // FIXED: explicitly return zero
}
Note