Thread: Why do we need "this" in C++?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why do we need "this" in C++?

    I understand "this" is for obtaining the object's address. And I understand that "this" is useful for assignment, for eg:
    Code:
    class A{
    A operator =(const A& rhs){
    if(this != &rhs){...}
    }
    };
    But why do I see people writing code like this:

    Code:
    class A{
    //..omit constructor...
    
    void foo1(){
    this->foo2(val_);
    }
    void foo2(int val){
    val++;
    }
    private:
    int val_;
    }
    Can't we just write

    Code:
    void foo1(){
    foo2(val_);
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by meili100 View Post
    Can't we just write

    Code:
    void foo1(){
    foo2(val_);
    }
    Yes we can.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by ZuK View Post
    Yes we can.
    Kurt
    Then what's the goodness to use "this" pointer, except in the example I gave? At least you don't need to type the 6 extra letters and at the mean time keep your code clear./

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by meili100 View Post
    Then what's the goodness to use "this" pointer, except in the example I gave? At least you don't need to type the 6 extra letters and at the mean time keep your code clear./
    What if an object needs to pass a pointer to itself to some other piece of code? "this" is the only way to get that pointer.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    this points to the object itself.
    So when you type function_name_here or variable_name_here, the compiler silently pushed this on the stack so that the data is modified in your own class.
    If you try calling a class without a this, it will try to read/write from 0, thus causing a crash. This is used to identify where in memory the class lies.
    Usually, you don't really need to use it in your class, but the compiler needs it.
    But this can also be used to check in a copy constructor or such, to make sure you aren't copying yourself into yourself.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't think I've ever had to use this when calling a member function. Maybe some people just go a little this crazy?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yep. It's not often very useful (except in rare cases, such as when you don't want the user to copy the same object into itself).
    It has its uses, but not very often.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by cpjust View Post
    I don't think I've ever had to use this when calling a member function. Maybe some people just go a little this crazy?
    Probably, when you type "this->" the ide will bring up a list of members, so you can use auto-complete.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I usually don't, but when I use the Refractor, it lists everything w/o needing to do this->
    It's so very useful! Love it!

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Some (very few) coding standards require explicit qualification of all member accesses using this.

    this can be used to disambiguate between a member and a local variable of the same name. (Yeah, bad practice to have on in the first place.)

    this is the only way to get a pointer or reference to yourself, in case you want to pass it somewhere else, e.g.
    Code:
    void Foo::bar()
    {
      baz(*this);
    }
    Finally, some advanced template code requires explicit qualification of member accesses with this. Don't ask.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another way for "this" pointer
    By dukebdx12 in forum C++ Programming
    Replies: 12
    Last Post: 06-23-2008, 10:52 PM
  2. Passing "this" as function parameter
    By pgavigan in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2007, 10:06 AM
  3. quick question concerning "this"
    By HIM in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2006, 07:17 PM
  4. the "this" pointer
    By faze in forum C++ Programming
    Replies: 7
    Last Post: 06-24-2005, 01:20 AM
  5. Replies: 2
    Last Post: 10-13-2001, 10:22 PM