top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is this pointer in c++? Give proper syntax with example.

+1 vote
140 views
What is this pointer in c++? Give proper syntax with example.
posted Aug 11, 2017 by Anirudha Sarkar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Type of ‘this’ pointer in C++

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* 
    }
};

Code 2

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

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* 
    }
};
answer Sep 14, 2017 by Manikandan J
Similar Questions
+2 votes

suppose we have an array of N natural numbers and asks him to solve the following queries:-
Query a:- modify the element present at index i to x.
Query b:- count the number of even numbers in range l to r inclusive.
Query c:- count the number of odd numbers in range l to r inclusive.

input:
First line of the input contains the number N. Next line contains N natural numbers.Next line contains an integer Q followed by Q queries.
a x y - modify the number at index x to y.
b x y - count the number of even numbers in range l to r inclusive.
c x y - count the number of odd numbers in range l to r inclusive.
I tried to solve using simple arrays but it isn't doing well for big constraints so I thought to use other DS with efficient algorithm so please explain appropriate algorithm.Thanks in advance.

...