×

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

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 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.

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

SDL library in C++ with Examples

SDL stands for Simple DirectMedia Layer. Using OpenGL and Direct3D, it is a cross-platform development library created to give users low-level access to audio, keyboard, mouse, joystick, and graphics hardware....

3 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

Print Table Using While Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Bit Manipulation in C++

The high-level language in which we communicate is not understood by the computer. As a result, there existed a standard mechanism for understanding any instruction sent to the computer. At...

5 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

2 minutes read.

C++ Output Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

4 minutes read.