You are here:

C++/this -> function


When you are writing code in a member function, the "variable" this contains a pointer to the current object. Any member element, _x, can be referenced in two ways:
_x = 10;
this->_x = 10;  // same as above.

then what situation using the-> would be better...?

can you give me a basis definition of thie-> funciton? i just a beginner of c++....

thank you

Dear Ken,
Thanks again for your question. Well first
of all, have a look at this detail of this pointer.

this Pointer

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.

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.

#include <iostream.h>
#include <string.h>

class Buf {
 Buf( char* s );
 Buf& operator=( const Buf & );
 void Display() { cout << buffer << endl; }
 char* buffer;

Buf::Buf( char* s ) {
 buffer = new char[ strlen( s ) + 1 ];
 strcpy( buffer, s );
Buf& Buf::operator=( const Buf &otherbuf ) {
 if( &otherbuf != this ) {
   delete [] buffer;
   buffer = new char[ strlen( otherbuf.buffer ) + 1 ];
   strcpy( buffer, otherbuf.buffer );
 return *this;
void main() {
 Buf myBuf( "my buffer" );
 Buf yourBuf( "your buffer" );
 myBuf = yourBuf;

Mostly the former version which you used is not
suitable in C++ style, mostly your

is preffered, because it is more clear and readable.
I hope this helps.
Best of luck,


All Answers

Answers by Expert:

Ask Experts




I can answer any question about functions,pointers,structures,object oriented programming basics of classes and data structures.My strong field is structured programming.


I have got 2 years experiece under C . I am able to answer about the structured concepts pointers to a little extent,OOP concepts. I have also experience in data Strucutres like Linked List, Stacks , Queues, Heaps, B Trees, Red Black Trees. I will try to satisfy with my knowledge. I am the Student of an expert here Martin, what i have learnt today, i just owe my every knowledge to him. He is the greatest.

©2017 All rights reserved.