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
#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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Pointer to an Array | Array Pointer
- What is a Pointer to a Null pointer
- How to declare a pointer to a function?
- Declare a C/C++ function returning pointer to array of integer pointers
- 'this' pointer in C++
- Multidimensional Pointer Arithmetic in C/C++
- When do we pass arguments by reference or pointer?
- Scope Resolution Operator Versus this pointer in C++?
- Opaque Pointer
- Passing by pointer Vs Passing by Reference in C++
- A C/C++ Pointer Puzzle
- Common Memory/Pointer Related bug in C Programs
- Sum of array using pointer arithmetic
- Reference to a pointer in C++ with examples and applications
- Passing Reference to a Pointer in C++
- C++ Program to count Vowels in a string using Pointer
- Trie Data Structure using smart pointer and OOP in C++
- Different ways to use Const with Reference to a Pointer in C++
- Data type of character constants in C and C++
- Can a C++ class have an object of self type?

