Login    New User    Forgot Password    Help      

The this pointer (C++ only)


The this pointer (C++ only)

The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a nonstatic member function f(). If you call the function x.f(), the keyword this in the body of f()stores the address of x. You cannot declare the this pointer or make assignments to it.

A static member function does not have a this pointer.

The type of the this pointer for a member function of a class type X, is X* const. If the member function is declared with the const qualifier, the type of the this pointer for that member function for class X, is const X* const.

A const this pointer can by used only with const member functions. Data members of the class will be constant within that function. The function is still able to change the value, but requires a const_cast to do so:

void foo::p() 
member = 1;                       // illegal     
 const_cast <int&> (member) = 1;   // a bad practice but legal   

A better technique would be to declare member mutable.

If the member function is declared with the volatile qualifier, the type of the this pointer for that member function for class X is volatile X* const. For example, the compiler will not allow the following:

struct A {    
int a;    
int f() 
{ return a++; } 

The compiler will not allow the statement a++ in the body of function f(). In the function f(), the this pointer is of type A* const. The function f() is trying to modify part of the object to which this points.

The this pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions.

For example, you can refer to the particular class object that a member function is called for by using the thispointer in the body of the member function. The following code example produces the output a = 5:

#include <iostream>  
struct X 
int a;  
void Set_a(int a) {        // The 'this' pointer is used to retrieve 
'xobj.a'      // hidden by the automatic variable 'a'      
this->a = a;    }    
 void Print_a() 
{ cout << "a = " << a << endl; } 
 };    int main() {    X xobj;   
 int a = 5;    

In the member function Set_a(), the statement this->a = a uses the this pointer to retrieve xobj.a hidden by the automatic variable a.

Unless a class member name is hidden, using the class member name is equivalent to using the class member name with the this pointer and the class member access operator (->).