Thread: can someone explain the this in the class ? i havent yet understood what it does

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    can someone explain the this in the class ? i havent yet understood what it does

    can someone explain the this in the class ? i havent yet understood what it does

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    this is a pointer to the invoking object. It is used often in overloaded-operator functions that need to return the object for stringing (i.e. n op m op r => n.op(m).op(r) ).

    This is why in your previous code, *this was used. That is the actual instance of the invoking object.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    yes but i need a good example

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you have
    myClass obj[10];

    And you invoke
    obj[5].func();

    Then the this pointer is the thing which tells you that it's obj[5] that needs to be updated, and not something else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM