18. What does mean reserved keyword this?

What does mean reserved keyword this?
Nusrat NuriyevAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Hmm, 'this' is a compiler-provided construct, so the isn't actually a declaration, but if it was a member, the declaration would be equal to declaring a pointer to the actual class. To stay in the above picture, that would be a

class MyClass {
private:
  MyClass* this;
};                         

Open in new window


And as a side effect we finally found a way where you would really use 'private' and it makes perfect sense ;o)
0
 
jkrConnect With a Mentor Commented:
'this' is a pointer that always points to the current object. It is passed as a hidden parameter to all non-static member functions of a class. You can use 'this' like any other pointer in C or C++, including all operatios and dereferencing, e.g.

class MyClass {
public:
    void foo(i) {
        MyClass clone = *this;
    }
};                         
                                          

Open in new window

0
 
Nusrat NuriyevAuthor Commented:
Could you explicitly show how this pointer might be declared?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Nusrat NuriyevAuthor Commented:
jkr, sarabande won't agree with you :)
0
 
jkrCommented:
Well, I think I have to live with that ;o)
0
 
sarabandeCommented:
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
0
 
Nusrat NuriyevAuthor Commented:
>>> 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?
0
 
sarabandeCommented:
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
0
 
Nusrat NuriyevAuthor Commented:
Thank you for your anwers
0
All Courses

From novice to tech pro — start learning today.