AppDividend
Latest Code Tutorials

C++ this Pointer Example | this Pointer In C++ Tutorial

0

C++ this Pointer Example | this Pointer In C++ Tutorial is today’s topic. A this” pointer is used to access the address of the object in its class definition. Since you can’t refer to an object in the definition before it’s creation “this” pointer comes at use. It helps in referring to the object address before it’s creation.

C++ this Pointer

this pointer holds the address of the current object, in the easy words, you can say that the this pointer points to a current object of the class.

#Problem with the member function definition

If we want to understand things clearly, let us look at a class definition. It consists of two things.

  1. Data members
  2. Member functions

When multiple objects are created using the same class definition, they have separate data members as per the definition, but they all use the identical copy of the member functions

Unlike the data members, there isn’t a separate copy for each object created using the class definition but a single one only.

This leads to a lot of confusion when working with the member functions. Since objects have different data members but ultimately with the same variable name as all of them have their different scopes, it becomes almost impossible to refer to a specific object’s data members.

#Solution using “this” pointer

If we want to solve this problem, “this” pointer is used in the member function definition. Rather than going by the variable’s name, we look for its address. Since the address is unique for each object’s data member, there’s no confusion with it.

But, the problem that arose is how to access the address of the data member of an object which hasn’t been created till now in the first place. 

To address this problem, we use “this” pointer, which automatically contains the address of the object as it is created. Using this, we can refer to the data members of the object in the member function definition even if it is not created till now.

#Program to show usage of “this” pointer

See the following program of this pointer.

#include <iostream>
using namespace std;
class ascii
{
private:
  char symbol;
  int value;

public:
  void assign(char symbol, int value)
  {
    this->symbol = symbol;
    this->value = value;
  }
  void print()
  {
    cout << symbol << endl;
    cout << value << endl;
  }
};

int main()
{
  int symbol = 'A';
  char value = 65;
  ascii obj;
  obj.assign(symbol, value);
  obj.print();
  return 0;
}

See the following output.

 

This Pointer In C++ Tutorial

See the following second example of this pointer.

#include <iostream>
using namespace std;
class Sample
{
private:
  int id;
  char grade;

public:
  void member1(int id, char grade)
  {
    this->id = id;
    this->grade = grade;
  }
  void member2()
  {
    cout << id << endl;
    cout << grade << endl;
  }
};

int main()
{
  int id = 12;
  char grade = 'D';
  Sample object1;
  object1.member1(id, grade);
  object1.member2();
  return 0;
}

See the following output.

 

C++ this Pointer Example

#Explanation

Now, in this example, we have two data members and two member functions. The problem is we have the same name for both the global variables in the main() function and the data members in member1() in the class definition.

So, it causes a problem on how to tell the compiler in which one to use while displaying it on the screen in the member2() definition.

For this we use, “this” pointer in the member1() definition. This tells the compiler that the object’s data member has to be set equal to the variables with the same name provided as the arguments.

And finally, the object’s data members are displayed on the screen with the member2() function. So this reduces the chaos with the same name variables using their addresses in the memory with the object creation.

We can use the ‘this’ pointer in the following scenarios.

#When the local variable’s name is the same as the member’s name

See the following program.

#include <iostream>
using namespace std;

class App
{
private:
  int x;

public:
  void setX(int x)
  {
    this->x = x;
  }
  void print() { cout << "x = " << x << endl; }
};

int main()
{
  App app;
  int x = 11;
  app.setX(x);
  app.print();
  return 0;
}

See the following output.

 x = 11

#If we want to return a reference to the calling object

When a reference to the local object is returned, the returned reference can be used to chain function calls on the single object. See the following program.

#include <iostream>
using namespace std;

class App
{
private:
  int x;
  int y;

public:
  App(int x = 0, int y = 0)
  {
    this->x = x;
    this->y = y;
  }
  App &setX(int a)
  {
    x = a;
    return *this;
  }
  App &setY(int b)
  {
    y = b;
    return *this;
  }
  void print() { cout << "x = " << x << " y = " << y << endl; }
};

int main()
{
  App app(5, 5);
  app.setX(11).setY(10);

  app.print();
  return 0;
}

See the following output.

x = 11 y = 10

Finally, C++ this Pointer Example | this Pointer In C++ Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.