Solved

18. What does mean reserved keyword this?

Posted on 2015-01-24
9
81 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 500 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 500 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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 33

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 33

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 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