the this pointer and other tasteless tales

This is a discussion on the this pointer and other tasteless tales within the C++ Programming forums, part of the General Programming Boards category; Hey everyone...quick question, when derefrencing the this pointer in a class with multiple private member variables...how do you make the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    the this pointer and other tasteless tales

    Hey everyone...quick question, when derefrencing the this pointer in a class with multiple private member variables...how do you make the derefrenced this point to a specific member variable...or can you not? I'm a littrle confused by the whole concept of the this pointer so I apologize if this is a stupid question...thanks a lot!-Chap(Oh great!, now I must really be a programmer. I spilled coffee all over my programming book...that will look really funny when I look back on it as a testament to my beginner days...)
    Oh...one more thing! Is it only possible to use the this pointer within class methods or can I use it in global functions also?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Use either (*this).name_of_variable or this->name_of_variable to get at a particular variable.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    To access a member from within a function of your class use:

    this->some_member

    or more simply,

    some_member

    The this pointer is just a pointer to the current object - you don't actually *need* to use it that often because you're allowed to use the second syntax above which is equivalent to the first. You might use the this pointer if you wanted to return the current object, e.g.

    PHP Code:
    //assignment operator
    MyClassMyClass::operator=(const MyClass &C) {
    //do the copy
    return *this//return ourselves.

    You can't use the this pointer in global functions because there is no this pointer - it makes no sense, because there's no "current object".
    Last edited by azteched; 12-28-2004 at 04:36 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    guys...your answers were beyond helpful...in fact you both the *beep* and deserve midwestern towns named in your honor, haha thanks a lot.-Chap

Popular pages Recent additions subscribe to a feed

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