Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy 'this' is a pointer and not a reference?
    text
    copied!<p>I was reading the answers to this question <a href="https://stackoverflow.com/questions/385297/whats-wrong-with-c-compared-to-other-languages">C++ pros and cons</a> and got this doubt while reading the comments. </p> <blockquote> <p>programmers frequently find it confusing that "this" is a pointer but not a reference. another confusion is why "hello" is not of type std::string but evaluates to a char const* (pointer) (after array to pointer conversion) – Johannes Schaub - litb Dec 22 '08 at 1:56</p> <p>That only shows that it doesn't use the same conventions as other (later) languages. – le dorfier Dec 22 '08 at 3:35</p> <p>I'd call the "this" thing a pretty trivial issue though. And oops, thanks for catching a few errors in my examples of undefined behavior. :) Although I don't understand what info about size has to do with anything in the first one. A pointer is simply not allowed to point outside allocated memory – jalf Dec 22 '08 at 4:18</p> <p>Is this a constant poiner? – yesraaj Dec 22 '08 at 6:35</p> <p>this can be constant if the method is const int getFoo() const; &lt;- in the scope of getFoo, "this" is constant, and is therefore readonly. This prevents bugs and provides some level of guarantee to the caller that the object won't change. – Doug T. Dec 22 '08 at 16:42</p> <p>you can't reassign "this". i.e you cannot do "this = &other;", because this is an rvalue. but this is of type T*, not of type T const . i.e it's a non-constant pointer. if you are in a const method, then it's a pointer to const. T const . but the pointer itself is nonconst – Johannes Schaub - litb Dec 22 '08 at 17:53</p> <p>think of "this" like this: #define this (this_ + 0) where the compiler creates "this_" as a pointer to the object and makes "this" a keyword. you can't assign "this" because (this_ + 0) is an rvalue. of course that's not how it is (there is no such macro), but it can help understand it – Johannes Schaub - litb Dec 22 '08 at 17:55</p> </blockquote> <p>My question is, why <code>this</code> is a pointer a not a reference? Any particular reason for making it a pointer?</p> <hr> <p>Some further arguments why <code>this</code> being a reference would make sense:</p> <ul> <li>Consider <code>Item 1</code> from <code>More Effective C++</code>: use references when it is guaranteed that we have a valid object i.e. not a NULL (my interpretation). </li> <li>Furthermore, references are considered safer than pointers (because we can't screw the memory up with a stray pointer). </li> <li>Thirdly, the syntax for accessing references (<code>.</code>) is a little bit nicer and shorter than accessing pointers (<code>-&gt;</code> or <code>(*)</code>).</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload