C++ this pointer

C++ this pointer

C++ language uses a unique keyword called “this” to represent an object that invokes a member function. this is a pointer to the object for which “this” function was called. for example The function calling A.max() set the pointer this to the address of the object A.
The starting address is the same as the address of the first variable A in the class structure

This unique pointer is automatically passed to a member function whenever it is called . The pointer this acts as an implicit argument to all the member functions. take e example

class ABC
{
int a;
…..
…..
};

in the above example private variable a can be used directly inside a member function like

item *it_ptr;

a=145;
we can also use the following statement in below to do same activity this->a=123;

since c++ programming permits the use of shorthand form a=145 we have not been using the pointer this explicitly so far. when we have been implicitly using the pointer this when overloading the operators using the member function re-call that when a binary operator overloaded by using a member function. we pass only one argument to the function. The other argument is implicitly pass using the pointer “this” . one of the important application of the pointer this is to return the object it points to. in the below take a example to understand

person & person :: greater(person & X)
{
if(x.age>age)
return x; // argument object
else
return *this; // invoking the object
}
suppose we invoke this function by the calling
max=A.greater(B);

The function will return the object B(argument object) if the age of the person B is greater than of A, otherwise this will return the object A (invoking object) using the pointer this, remember The dereference operator * produces the contents at the address contained in the pointer.

A complete program to use of the this is given in below

this Pointer
#include <iostream>
#include <cstring>
using namespace std;
class person
{
char name[20];
float age;
public:
person(char *s, float a)
{
strcpy(name,s);
age=a;
}
person & person :: greater(person &x)
{
if(x.age>=age)
return x;
else
return *this;
}

void display()
{
cout << "Name:" << name << "\n"
<< "age:" << age << "\n";
}
};
int main()
{
person p1("john",37.50),
p2("ahmed",29.0),
p3("Hebber",40.25);
person p=p1.greater(p3); //p3.greater(p1)
cout << "Elder person is: \n";
p.display();
p=p1.greater(p2); // p2.greater(p1)
cout << "Elder person is: \n";
p.display();
return 0;
}

Elder person is:
Name: Hebber
Age: 40.25
Elder person is:
NAme: John
Age: 37.5

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise