"this" - I've seen explanations of it, yet I am still in the dark.

This is a discussion on "this" - I've seen explanations of it, yet I am still in the dark. within the C++ Programming forums, part of the General Programming Boards category; I have read the tutorials, heard explanations, etc; yet I cannot grasp the understanding of the "this" keyword. Any help ...

  1. #1
    Unregistered
    Guest

    "this" - I've seen explanations of it, yet I am still in the dark.

    I have read the tutorials, heard explanations, etc; yet I cannot grasp the understanding of the "this" keyword.

    Any help would be greatly appreciated.

  2. #2
    kimberleyp
    Guest
    Have you had any experience with class usage before? if not, please consult the FAQ section. Otherwise, just read on...

    the 'this' pointer is pre-defined, and it points to the CURRENT CLASS INSTANCE, or object. For example, with a class CBox, if you call a member function within this class (i.e. CBox::CBox(); ), the 'this' pointer will be set to the address of the CBox object which you created (i.e. CBox myBox; ).

    Here it is in code:

    //---------------------------------------------------------------------------------
    class CBox
    {
    public:
    CBox()
    {
    cout << " 'this' pointer in CBox::CBox(): " << this << endl;
    }
    };

    int main()
    {
    cout << " 'this' pointer in main(): " << this << endl;
    CBox myBox;
    return 0;
    }
    //---------------------------------------------------------------------------------

    as you can see, the 'this' pointer is different when referenced within the CBox class constructor compared to in main(). 'this' is useful in many ways, such as in overloaded assignment operator functions, where you MUST check first to see whether or not the class object to be copied occupies the same address in memory. if so, it sets 'this' to the parameter, then returns. an example of this follows:

    //---------------------------------------------------------------------------------
    CBox CBox:perator =(const CBox& rBox)
    {
    if(this == &rBox) // Is address of arg. the same?
    return *this; // Return contents of this ptr.

    tLength = rBox.tLength; // Initialize data members
    tWidth = rBox.tWidth; //
    tHeight = rBox.tHeight; //

    return *this; // Return de-referenced ptr.
    }
    //---------------------------------------------------------------------------------

    other uses include just finding the address of the current class object, and using it with the MS compiler at design-time to query ths current members of the class without using the project window (that's fairly subtle, but useful, nonetheless).

    i hope you now have a better understanding of how to use the 'this' pointer!

    Regards,
    Peter Kimberley
    peter_kimberley@hotmail.com

  3. #3
    Unregistered
    Guest
    Ahhh, I understand it now Thanks kindly for your time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Nearest value" algorithm?
    By Boxknife in forum C Programming
    Replies: 10
    Last Post: 06-27-2009, 12:42 AM
  2. dark side of global variables
    By MK27 in forum C Programming
    Replies: 18
    Last Post: 12-09-2008, 03:14 AM
  3. Question for people who use or have used the Dark SDK
    By Frantic- in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2005, 08:09 PM
  4. The Backdrop Story For My Next Game
    By TechWins in forum Game Programming
    Replies: 8
    Last Post: 06-25-2002, 06:45 PM
  5. The Dark Legends of Shazow
    By shazow in forum Game Programming
    Replies: 18
    Last Post: 06-06-2002, 10:48 AM

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