Solved

18. What does mean reserved keyword this?

Posted on 2015-01-24
9
74 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
 

Author Comment

by:Nusrat Nuriyev
ID: 40571441
jkr, sarabande won't agree with you :)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

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

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 32

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now