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

This is a discussion on can someone explain the this in the class ? i havent yet understood what it does within the C++ Programming forums, part of the General Programming Boards category; can someone explain the this in the class ? i havent yet understood what it does...

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

    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
    902
    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
    306
    yes but i need a good example

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21