Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

18. What does mean reserved keyword this?

Posted on 2015-01-24
9
Medium Priority
?
101 Views
Last Modified: 2015-01-27
What does mean reserved keyword this?
0
Comment
Question by:Nusrat Nuriyev
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 2000 total points
ID: 40568220
'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
 

Author Comment

by:Nusrat Nuriyev
ID: 40570902
Could you explicitly show how this pointer might be declared?
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 40571041
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Nusrat Nuriyev
ID: 40571441
jkr, sarabande won't agree with you :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 40571451
Well, I think I have to live with that ;o)
0
 
LVL 35

Expert Comment

by:sarabande
ID: 40572395
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
 

Author Comment

by:Nusrat Nuriyev
ID: 40572536
>>> 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
 
LVL 35

Expert Comment

by:sarabande
ID: 40572721
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
 

Author Comment

by:Nusrat Nuriyev
ID: 40574290
Thank you for your anwers
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question