Thread: this->sucks

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    this->sucks

    Nothing personal to *this or anything, but why exactly would you need to use this? I mean, if there's a member function, couldn't you just use the functions name? What advantage does this have?

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    wow, that's not very well thought out. "this" gives you the ability to tell someone else where you are. Imagine you are adding yourself to a list, you'ld use your pointer. Imagine you are giving a child object a "callback" pointer. You would give it your pointer. That is what "this" is for and not just to call your own member functions.

    oh and...

    you->suck

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Well, this is from more of a Java perspective, but what I use 'this' for mostly is to call overloaded/overriddden constructors or base class constructors.
    I've got too many languages in my head, hard to keep them straight sometimes. For example, in C/C++, a semi-colon marks the end of a statement. In Dataflex/XBase, it means the statement continues on the next line. Aaaarrrgghh!
    Truth is a malleable commodity - Dick Cheney

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    this enables you to return a reference or pointer to the current object. Other than that, I don't believe it fundamentally enables things that couldn't be done without it.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    "this" is important because not everything should be done from outside an object.

    Example one....

    In windows programming, if you have a class that represents a window the on create might look something like this...
    Code:
    CreateWindow(blah blah blah, this); // This puts the pointer to you in the window callback
    Then your callback will use the pointer to access the object You can use SetWindowLong to stick the pointer permanently in GWL_USERDATA


    Example two.

    if you want creation of an object to store that instance in a global list for instance. The only way for a constructor to put himself in a list is to have access to his own pointer. Something like this:
    Code:
    ClassName::ClassName()
       {
       g_list.Add(this);
       }
    
    ClassName::~ClassName()
       {
       g_list.Remove(this);
       }
    It's pretty simple actually. "this" is necessary.

Popular pages Recent additions subscribe to a feed