Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Type of ‘this’ pointer in C++

  • Difficulty Level : Medium
  • Last Updated : 27 Nov, 2018

In C++, this pointer is passed as a hidden argument to all non-static member function calls. The type of this depends upon function declaration. If the member function of a class X is declared const, the type of this is const X* (see code 1 below), if the member function is declared volatile, the type of this is volatile X* (see code 2 below), and if the member function is declared const volatile, the type of this is const volatile X* (see code 3 below).

Code 1

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




#include<iostream>
class X {
   void fun() const {
  
    // this is passed as hidden argument to fun().
    // Type of this is const X* const
    }
};

Code 2




#include<iostream>
class X {
   void fun() volatile {
  
     // this is passed as hidden argument to fun().
     // Type of this is volatile X* const
    }
};

Code 3




#include<iostream>
class X {
   void fun() const volatile {
  
     // this is passed as hidden argument to fun().
     // Type of this is const volatile X* const
    }
};

References:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!