Thread: Clarifying the "this" pointer

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    Clarifying the "this" pointer

    I understand we return by address or return by reference in order to reduce memory usage by only returning the memory location of the object.

    For the two snippets, the former is a Calculator object and it is returning each basic arithmetic by reference, what I am not clear about is why it's '*this' and not 'this'? Is it mixing references and pointers, so I mean "this" is a hidden pointer, so
    Code:
    return *this;
    means it is dereferencing it before it can be returned? While with the struct example, there is no use of asterisk in front of the member returned.

    Code:
    class Calc 
    { 
    private: 
        int m_nValue; 
      
    public: 
        Calc() { m_nValue = 0; } 
      
        Calc& Add(int nValue) { m_nValue += nValue; return *this; } 
        Calc& Sub(int nValue) { m_nValue -= nValue; return *this; } 
        Calc& Mult(int nValue) { m_nValue *= nValue; return *this; } 
      
        int GetValue() { return m_nValue; } 
    };
    (VS)

    Code:
    // This struct holds an array of 25 integers 
    struct FixedArray25 
    { 
        int anValue[25]; 
    }; 
      
    // Returns a reference to the nIndex element of rArray 
    int& Value(FixedArray25 &rArray, int nIndex) 
    { 
        return rArray.anValue[nIndex]; 
    } 
      
    int main() 
    { 
        FixedArray25 sMyArray; 
      
        // Set the 10th element of sMyArray to the value 5 
        Value(sMyArray, 10) = 5; 
      
        cout << sMyArray.anValue[10] << endl; 
        return 0; 
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    For the two snippets, the former is a Calculator object and it is returning each basic arithmetic by reference, what I am not clear about is why it's '*this' and not 'this'?
    O_o

    You kind of answered your own question.

    In the context of implementing a class function, `this' is a pointer and `*this' is a reference (C++) which means that `this' isn't a reference (C++) and `*this' isn't a pointer.

    So, looking at the other example, `anValue' is an array decaying into a pointer while `anValue[nIndex]' is a reference (C++).

    In both cases, the pointer is "dereferenced" to obtain a reference.

    Yes, C++ is awesome.

    [Edit]
    It may help you to cope with this situation if you conceive `anValue[nIndex]' as being syntaxctical sugar for `*(anValue + nIndex)'.
    [/Edit]

    Soma

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    So basically C++ decided to use "this" as a pointer and the C++ standard decided to stick w/ it rather than use "this" as a reference? So that's why we need to mix pointers and reference (which is just implicity derferenced pointers). Also, we don't return objects instances (structs/classes) by address b/c of ugly dereferencing notation, is that right? I'll test this.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by monkey_c_monkey View Post
    So basically C++ decided to use "this" as a pointer and the C++ standard decided to stick w/ it rather than use "this" as a reference?
    That's nonsense. this has always been a pointer as far as C++ is concerned.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Yeah, this isn't a Java forum.

    Soma

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    'this' was a pointer due to its early days of "Cfront" which simply converted C++ code to C and then compiled that with a regular C compiler.
    It is regarded of one of the unfortunate mistakes of the language, alongside throw specifications.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    It is regarded of one of the unfortunate mistakes of the language, alongside throw specifications.
    I wouldn't agree that `this' being a pointer was a mistake. There were two choices (make it a pointer or a reference) and they are almost interchangeable. They picked one, which happens to be the more general case (a pointer can be NULL and it can be modified, a reference cannot). That made some sense, at the time, since there was debate in some parts of the OO community at the time about whether it made sense for class operations to be able to modify their identify: reassigning a pointer is possible, reseating a reference is not.

    People who have learned other languages (Java, C#, etc) as their first language invariably complain when the second language they learn does things differently to their first. And that is essentially what monkey_c_monkey is doing.


    I agree that exception specifications were a mistake, since it makes functions unusable in a lot of cases. However, C++ was not the only language that incorporated such a mistake.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Stroustrup is "wishy-washy"; at various times he has stated that he would never have made `this' a reference because he sought familiarity with a feature from a different language, but he also stated that had he not been swamped with new features and concerns relating to the coming standard meetings he would have made the breaking change with the existing body of code and made `this' a reference.

    The "exception specification" idea was a good one.

    The problem is that reality rarely plays nicely with good ideas.

    If they did what people think they do, versus what they actually do, they'd be awesome, but that is almost impossible so we have a mess.

    However, C++ was not the only language that incorporated such a mistake.
    A mistake that's been made from that point many times solely because C++ is seen as a language to target for familiarity instead of features.

    *shrug*

    At some point we, a programmers, may get lucky and find a language that has the features of C++ without the awful syntax and inclusion model.

    Soma

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by phantomotap View Post
    Stroustrup is "wishy-washy"; at various times he has stated that he would never have made `this' a reference because he sought familiarity with a feature from a different language, but he also stated that had he not been swamped with new features and concerns relating to the coming standard meetings he would have made the breaking change with the existing body of code and made `this' a reference.
    Such is the process of modifying a language which is in use, particularly if a standards body is involved. Theoretically, it would be nice if this was a reference. Practically, this as a pointer does not prevent anyone from doing anything (and, of course, it keeps critics of C++ and zealots of other languages happy )

    Quote Originally Posted by phantomotap View Post
    The "exception specification" idea was a good one.

    The problem is that reality rarely plays nicely with good ideas.
    I'm not arguing that exception specifications are a bad thing. Their utility in C++ is not so hot.

    Quote Originally Posted by phantomotap View Post
    At some point we, a programmers, may get lucky and find a language that has the features of C++ without the awful syntax and inclusion model.
    There have been a few attempts at such languages. Some of those languages are quite good, but they also have their foibles ....

    Pending some perfect language, we get to enjoy being lectured or questioned by fans of other languages about the deficiencies of C++, because they assume we don't know about them.

    Real-world programming language design is about trade-offs between practicality and idealism, not absolute concepts of "good" or "bad".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'm not arguing that exception specifications are a bad thing.
    ^_^

    I know you aren't arguing about anything.

    I was just throwing my own thoughts into the thread.

    There have been a few attempts at such languages.
    Truth. I'm fond of Ada myself.

    Pending some perfect language, we get to enjoy being lectured or questioned by fans of other languages about the deficiencies of C++, because they assume we don't know about them.
    Always worth a giggle...

    Soma

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by phantomotap View Post
    At some point we, a programmers, may get lucky and find a language that has the features of C++ without the awful inclusion model.
    Isn't that what many would say D is?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    Isn't that what many would say D is?
    Depends on your definition of "many".

    D is like a lot of other languages that have some derivation from C++: some features removed that the creator considered "bad" (e.g. multiple inheritence), other features added that the creator considered "good" (e.g. initialisation of everything). If you happen to agree with the philosophy of the creator, then any of those languages might be considered the ultimate. If not, .....

    Historically, there was BCPL. Then B. Then C. That suggests the name of the next language should actually (or eventually?) be P. The ultimate will be L, not D.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If we continue down this path, there will never be a "perfect" language. Every feature that is removed from C++ (in some spinoff language, of course) would probably upset lots of C++ programmers.
    And yet, adding new things that forces programmers to do things certain ways will also upset people, and might even in some cases create inferior code.
    Anyway, I find "D" to be a failed language. Forced garbage collection, no multiple inheritance... it's turning into Java, so we might as well use that awful language.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Isn't that what many would say D is?
    O_o

    Isn't that what many would say Java is?

    Isn't that what many would say C# is?

    Isn't that what many would say Haskell is?

    Isn't that what many would say Common LISP is?

    *three days later*

    Isn't that what many would say Python is?

    Really though, that was kind of a silly question.

    If you take features away (or simply never add them), use a similar inclusion model, or a foundation of C (heavily mixed) syntax you don't get what I've asked for in a programming language.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-08-2010, 01:47 AM
  2. "*x + *x++" Address/Pointer "?"
    By Marth_01 in forum C Programming
    Replies: 10
    Last Post: 11-05-2008, 04:33 AM
  3. "Pointer" plus "Number"
    By Petike in forum C Programming
    Replies: 11
    Last Post: 10-17-2008, 12:24 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM