5. this keyword #

Created Thursday 08 April 2021

‘this’ keyword #

A class may need to refer to itself. Solve this problem - Making setters/core functions. Approaches:

  1. To avoid naming issues, we have to come up with new parameter names for data members in all our member functions.
class A{
	String name;
	void sayHello(String name_for_hello) // this name has to be invented for every attribute in all functions, which is a pain
	{
		cout << "Hi, " << name_for_hello << "\nI am " << name << endl;
	}
};
  1. If we use the same name, then our data members will be shadowed by the formal parameters of the function.
class A{
	String name;
	void sayHello(String name)
	{
		cout << "Hi, " << name << "\nI am " << name << endl; // both refer to the argument, not A.name
	}
};
  1. If we use the object name to differentiate the data members, it will be absurd, because
    1. The class does not know an object before creation.
    2. The object name will have to be changed for each instance.
class A{
	String name;
	void sayHello(String name_for_hello)
	{
		cout << "Hi, " << obj.name << "\nI am " << name << endl; // this is absurd, object name can be anything
	}
};

Solution: We have a keyword ‘this’ which stores the pointer to the current object. This solves all the problems. Syntax: this -> data_member/function About **this: **It is available only inside the class, and only for non-static methods. Note: