In my previous article, I have explained what is ‘this pointer’ and when we need to use ‘this pointer’ in C++ programming. If you want you can check this article “ Use of this pointer in C++“. It will help to revise all the basic concepts related to this pointer.
The aim of this blog post is to explain the “Type of this pointer in C++”. I will also write some example codes for better understanding. Before starting the articles I want to mention a short summary of this pointer which helps in understanding.
A “this pointer” is a prvalue (“pure” rvalues) accessible only within the nonstatic member functions of a class, struct, or union type. The value of ‘this pointer’ is the address of the object for which the member function is called.
The ‘this pointer’ is mostly used implicitly. But it’s legal to use this pointer explicitly when referring to members of the class. Let’s see an example,
class Test { //member variable int m_data; public: //non-static member function void value(int a); }; void Test::value( int a ) { m_data = a; // These three statements this->m_data = a; // are equivalent (*this).m_data = a; }
I think now you are quite familiar with this pointer. Now, we can see the type of this pointer in C++.
Type of this pointer in C++:
The type of this pointer in a member function of class X is X*. But the this pointer’s type can be modified in the function declaration by the const and volatile keywords. So let’s see the effect of the CV (const and volatile) qualifier on the type of this pointer in C++.
Effect of “const” on this pointer:
If the member function is declared const, the type of this pointer is const X*. To declare a member function const, we need to add the const keyword after the function argument list. Consider below example,
class X { void test() const { // Type of this is const X* } };
The preceding example C++ code declares a member function, test, in which this pointer is treated as a const pointer to a const object. Calling this a const pointer is not totally true because it is prvalue (Which means “this pointer” is nonmodifiable, and assignment to this pointer is not allowed).
You should always remember that the adding const keyword in function declaration always modifies the object pointed to by this pointer, not the pointer itself.
Effect of “volatile” on this pointer:
If the member function is declared volatile, the type of this pointer is volatile X*. To declare a member function volatile, we need to add the volatile keyword after the function argument list. Consider below example,
class X { void test() volatile { // Type of this is volatile X* } };
The preceding example C++ code declares a member function, test, in which this pointer is treated as a const pointer to a volatile object.
If you already know how to program and just want to learn C++, you can start with the C++ Fundamentals course. The good thing is that TRIAL IS FREE
Duration: Approx. 43 hours
Rating: 4.6
Effect of const, volatile together on this pointer:
If the member function is declared const volatile, the type of this pointer is const volatile X*. To declare a member function const volatile, we need to add the const volatile keyword after the function argument list. Consider below example,
class X { void test() const volatile { // Type of this is const volatile X* } };
The preceding example C++ code declares a member function, test, in which this pointer is treated as a const pointer to a const volatile object.
Note The CV qualifiers (const or volatile) used in the member function at the time of declaration, apply to the class instance which points by this pointer in the scope of that function.
The semantics of CV modifiers
Below I have mentioned the semantics of the CV qualifier. If you want, you can follow the below articles,
Modifier | Meaning |
---|---|
const |
Can’t change member data; can’t invoke member functions that aren’t const . |
volatile |
Member data is loaded from memory each time it’s accessed; disables certain optimizations |
You can check MCQs on this pointer.
Recommended Articles for you:
- this pointer in C++ programming with some example code
- Amazing list of Gifts for Programmers, You must.
- Operator Overloading in C++ with some FAQ.
- Introduction of reference in C++.
- A brief introduction of pointers.
- Difference between pointers and references.
- Use of mutable keywords in C++.
- Best electronic kits for programmers.
- References and const in C++ with example programs.
- C++ Interview Questions with Answers.
- List of some Best C++ Books, you must see.