Type of this pointer in C++ with programming examples

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

Grab your free Trial, today

 

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,

SEMANTICS OF THESE MODIFIERS
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.

C++ MCQ

C++ this pointer MCQ: Multiple Choice Questions and Answers on C++ this pointer

1 / 26

What is the purpose of the "this pointer" in C++?

2 / 26

The address of the object _________________

3 / 26

What is the significance of using the const qualifier in the following member function declaration:

int get() const;

4 / 26

The this pointer is accessible __________________

5 / 26

Which is the correct interpretation of the member function call from an object, object.function(value);

6 / 26

What happens if you attempt to modify a data member using the this pointer in a const member function?

7 / 26

When is the this pointer required to be used explicitly in a member function?

8 / 26

The this pointers _____________________

9 / 26

Earlier implementations of C++ ___________________

10 / 26

In a const member function, the type of the this pointer is treated as:

11 / 26

In a member function, if a local variable has the same name as a data member, how can you refer to the data member using the this pointer?

12 / 26

Which among the following is true?

13 / 26

What does the this pointer allow you to access in a member function?

14 / 26

Which statement is true for this pointer?

15 / 26

Which among the following is true?

16 / 26

Which modifier affects this pointer?

17 / 26

Which statement is true?

18 / 26

The result of sizeof() function __________________

19 / 26

In which type of member function is the "this pointer" treated as a pointer to a constant object?

20 / 26

This pointer can be used directly to ___________

21 / 26

How is the this pointer affected when a non-static member function is declared as volatile?

22 / 26

Whenever non-static member functions are called _______________

23 / 26

Which is the pointer which denotes the object calling the member function?

24 / 26

Which operator can be overloaded using the this pointer?

25 / 26

Which statement is true?

26 / 26

Which syntax is used to guard against self-reference?

 

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *