Link to home
Start Free TrialLog in
Avatar of Nusrat Nuriyev
Nusrat NuriyevFlag for Azerbaijan

asked on

18. What does mean reserved keyword this?

What does mean reserved keyword this?
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nusrat Nuriyev

ASKER

Could you explicitly show how this pointer might be declared?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jkr, sarabande won't agree with you :)
Well, I think I have to live with that ;o)
if you try to compile the code of the accepted solution, you will get a compiler error because 'this' is a reserved keyword and cannot be used for a member variable.
 
you should accept one of the other comments in order to keep the knowledge base clean.

It is passed as a hidden parameter to all non-static member functions of a class.
that is not true for most compilers. if using 'this' in a statement, it will be compiled and it is implementation dependent how the current pointer of an object was provided at runtime. mostly pointers to objects and functions were provided by means of (virtual) tables rather than by passing a pointer on the stack.

the main purpose of 'this' is to give a member function the chance to return a reference to itself. that is necessary for operations which can be used in a cascading way like assignment operator:

MyObject a, b, c, d(5);
a = b = c = d;

Open in new window


the above only works cause MyObject has function 'MyObject & operator=(const MyObject &)' defined (explicitly or implicitly by the compiler) which has 'return *this;' as return statement. because of that the above code resolves as

a.operator=(b.operator=(c.operator=(d)));

Open in new window



as a side effect we finally found a way where you would really use 'private' and it makes perfect sense

'this' can be accessed by the class and its base classes in private, protected, and public member functions.  it always is a pointer to the currently instantiated object (which always exists since you can not instantiate abstract classes). a private member which stores a redundant copy of this pointer doesn't add any advantages nor disadvantages. it is simply redundant. note, even a public member function which returns 'this' would not violate encapsulation nor data hiding, since the caller of this function already has access to the object and therefore to the 'this' of the object. and of course all the restrictions regarding private or protected access of member functions remain unaffected regardless whether you were using a returned 'this' pointer or call it by using the variable, which is a pointer or reference or the object itself.

Sara
>>> mostly pointers to objects and functions were provided by means of (virtual) tables rather than by passing a pointer on the stack.

0. Still do not get this part. Could you explain in any way that I would understand what is virtual table and pointer on the stack and what is the difference?

>>> and of course all the restrictions regarding private or protected access of member functions remain unaffected regardless whether you were using a returned 'this' pointer or call it by using the variable, which is a pointer or reference or the object itself.

1. Could you provide an example?

>>> it always is a pointer to the currently instantiated object (which always exists since you can not instantiate abstract classes)

2. There is no 'this' pointer on abstract classes simply because it can not be instantiated?
the stack is used to store internal pointers to argument variables and local (local for the function) variables. a virtual table is  a 2-dimensional internal storage where associations of pointer to object and pointers to functions were stored. those tables are helpers at execution time (not at compile time) to call the right virtual function in case of a derived class instance. for a c function only a pointer to function was required for a call. a c++ function always needs two pointers, one for the object and one for the member function. if you call a member function via function pointer you always need to add explicitly a pointer to object to perform such a call. the first c++ compilers were made by turning c++ member functions to c functions and simply passing the pointer to object as 1st argument to all function calls (see first comment of jkr). this worked for all non-virtual member functions. for classes with virtual functions, an object may consist out of different parts which not necessarily use the same pointer to object. here the decision which function and which object pointer must be used cannot happen at compile time but at execution time. for that the virtual tables were used.

1. Could you provide an example?

assume you have class MyObject which provides a public function get_this() that returns 'this' pointer. then in your code the following calls to member function are equivalent:

void f(MyObject * pobj)
{
       ....
       pobj->anymemberfunction();
       pobj->get_this()->anymemberfunction();

Open in new window


void x()
{
     ....
     MyObject obj;
     obj.anymemberfunction();
     obj.get_this()->anymemberfunction();
     (&obj)->anymemberfunction();

Open in new window



void y()
{
     ....
     MyObject obj;
     MyObhect & objref = obj;
     objref.anymemberfunction();
     objref.get_this()->anymemberfunction();
     (&objref)->anymemberfunction();

Open in new window


the compiler would complain if you try to call a protected or private member function for all the statements above regardless whether you were using the variable or get the this pointer of the variable by function call.

There is no 'this' pointer on abstract classes simply because it can not be instantiated?

there is not a pointer at all for abstract classes since the compiler would refuse to compile a statement that tries to instantiate an abstract class. so instances of abstract classes only can exist as parts of a derived class which implemented all pure virtual functions of the abstract class. if there are no multiple base classes involved, most compilers would only use one pointer value for all this pointers. so if C was derived from B and B was derived from A then A::this, B::this, C::this have all the same pointer value. nevertheless if you have a pointer pa to A and call a virtual function f by pa->f(), you would call C::f() if the object behind 'pa' was a C object and if C has implemented function f.

Sara
Thank you for your anwers