KEYWORD THIS

In case you’re wondering how really is working the calling of member functions. In the end the computer on his lowest level doesn’t know anything about classes and functions because it manages with simple terms such as addresses, pointers and numbers. Objects are data types of very high level of abstraction and they have to be presented with elementary data types in computer program code. The trick is to, with every calling of a member functions of a class, hidden parameter forwards a pointer to an objects to which the member belongs. That hidden parameter can be accessed inside of a functions with the help of keyword this. Compiler during the analysis of a function (form previous post) “scalarmultiplication()” it actually interprets as:
void Vector::scalarmultiplication(float scalar){
       this -> nx *= scalar;
       this -> ny *= scalar;
}
and calling of member functions over the object v is interpreted as
Vector::scalarmultiplication(5, &v);
The major advantage of C++ programming language lies precisely in that the translator is hiding form the user and accessing over the pointer is done on his own. Whit use of modern programs the use of keyword this isn’t so often but in some specific cases it will be necessary inside member functions to know the address of the object for which the part was called. That can and must be done using a keyword this. It is necessary to mention that keyword this must not be declared as a formal parameter of member function because this keyword is automatically available for every member.
Keyword this is often used when we need to bring back the pointer or reference to the object. To demonstrate this we will expand the class vector with the function addwith() which  add up a vector with some other vector.
#include "stdafx.h"
#include "iostream"
using namespace std;

class Vector {
public:
       float ax, ay;
       Vector &Multiplywithscalar(float scalar);
       Vector &Addwith(float zx, float zy);
};

Vector &Vector::Multiplywithscalar(float scalar){
       ax *= scalar;
       ay *= scalar;
       return *this;
}
Vector &Vector::Addwith(float zx, float zy){
       ax += zx;
       ay += zy;
       return *this;
}
int _tmain(int argc, _TCHAR* argv[])
{
       Vector v;
       v.ax;
       v.ay;
       v.Multiplywithscalar(4).Addwith(5, -7);
       return 0;
}
Operator .is executed from left to right thereby achieving the desired operation sequence: first for vector v the function Multiplywithscalar() is called which for result returns reference on vector with new value and after the function Addwith() is called.
Keyword this is also used during the creation of objects which are used as storage for other objects for example doubly linked list.
class listelements{
private:
       listelements *next, *previous;
public:
       void Add(listelements*New);
};
void listelements::Add(listelements *New){
       New->previous = next;
       New->previous = this;
       next->previous = New;
       next = New;

}
Share on Google Plus