×

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to a member function when it is called. Consider the following simple example:
class Student{  
           int id;  
           .....  
           .....  
};
The variable 'id' can be used directly inside a member function, as
id=10;
The above initialization statement can be written using 'this' pointer, as
this->id=10;
C++ internally use the initialization of id=10; as this->id=10; Another key feature of the 'this' pointer is to return the object on which it is pointing. For example
return *this;
The above statement inside member function returns the object which is invoking the member function.

C++ 'this' Example 1

#include<iostream>  
using namespace std;  
class Student  
{  
private:  
   int id;  
public:  
   void setId (int id)  
   {  
       this->id = id; //retrieve the object's 'id' hidden by the private 
variable 'id'  
   }  
   void printId() { cout << "id = " << id << endl; }  
};  
int main()  
{  
   Student obj;  
   int id = 10;  
   obj.setId(id);  
   obj.printId();  
   return 0;  
}
Output:
id=10

C++ 'return *this' Example 2

#include<iostream>  
#include<cstring>  
using namespace std;  
class student  
{  
private:  
  char name[20];  
  int mark;  
public:  
  student(char *n, int m){  
    strcpy(name,n);  
    mark = m;  
  }  
  student  greater(student & a){  
    if(a.mark>=mark){  
        return a; //return when s1.greater(s2); is call  
     }  
    else{  
        return *this; //return when s1.greater(s3); is call  
    }  
  }  
  void display(void){  
    cout<< "Name: "<<name<<endl<<"Mark: "<<mark<<endl;  
  }   
};  
int main()  
{  
  student s1("Ajay",70);  
  student s2("Lucky",60);  
  student s3("Raj",80);  
  student s = s1.greater(s2);  
  cout<< "Greater mark: "<<endl;  
  s.display();  
  s = s1.greater(s3);  
  cout<< "Greater mark: "<<endl;  
  s.display();  
  return 0;  
}
Output:
Greater mark: 
Name: Ajay
Mark: 70
Greater mark:
Name: Raj
Mark: 80

Related Topics

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

Diamond Pattern in C++ using For Loop

For Loop: A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times. The syntax of for loop: In C++, a for...

5 minutes read.

C++ Functions

In other programming languages, a function is referred to as a process or a subroutine. We can design functions to execute any task. A function can be used repeatedly. It...

5 minutes read.

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

C++ Polymorphism

Polymorphism refers to the fact that something exists in several forms. Polymorphism, in simple terms, is the ability of a message to be presented in several formats. A real-life example...

4 minutes read.

Depth First Search Program to Traverse a Graph in C++

Depth First Search (DFS) is a technique that is used for transversing the graph. The process of Depth First Search (DFS) starts from the root node and then next comes...

6 minutes read.

For Loop Examples in C++

For Loop A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax of for loop In C++, a...

6 minutes read.

C++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

Advantage and disadvantage friend function C++

Friend Function: - A friend function in C++ is a function that can access the private, protected, and public members of a class. In C++, a friend function is a function that...

2 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 minutes read.

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

C++ Virtual Function

A virtual function is such function which is declared inside the base class and redefined by the derive class. C++ uses a virtual keyword to make a function as a virtual function. The virtual...

1 minute read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

7 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.