Thread: this is one "classy" thread

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    176

    this is one "classy" thread

    ok, so the title is a bad joke, anyways i am reading a tutorial on classes and well here's what it says
    every object has a special pointer call "this", which refers to the object itself. So the members of the Dog class can be referred to as this->age or this->weight, as well as, age or weight. If there is no ambiguity, no qualification is required. So in the getWeight method, "weight" can be used instead of "this->weight". In the setWeight method an ambiguity exists. Since the parameter passed is "weight" and there is a class member "weight", the "this" pointer must be used. Finally, a note about syntax. If "this" is a pointer to a class, then the member selection operator, "->", can be used to access the contents of its members.
    the tutorial is here and i was wondering whats up with the this-> pointer?

    edit: i wasn'tvery clear before, i want to know why i would need this-> in my code?
    Last edited by sreetvert83; 09-09-2005 at 08:55 PM. Reason: not clear
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    If you have two variables, one from a parameter, and one from a class, then to access the class one you'd use this->. I don't know how that wouldnt cause an error, or why a programmer would do that, so I dont find the advice useful.

    Most of the time I use 'this' in operator overloading, because you are taking 2 objects (usually), the one you are editing, and the one a reference is passed to (ie. ThisObject = ThisObject + AnotherObject). The AnotherObject would have a reference passed to the operator+, of the object ThisObject, and the operator+ would return it so itd be ThisObject = _______, so what do you want it to equal? You are altering in ThisObjects operator+, and therefor probably altering ThisObject, so how do you return the class you're processing in if you dont have a reference passed of it or anything? 'this'. Which results in ThisObject = ThisObject (but after the processing, adding of ThisObject and AnotherObject). Its used like return this; in this case, although it could be return this->total; but of course I said I dont see the point of that, just return total; if you need ot.

    Think of the class, basicly it can access the object of itself without needing to create it (ThisClass ThisObject) and of course it contains the data of the object (not a blank one). Then it can do what it wants with the object of itself, like send it as a parameter, return the object, maybe create a copy and change it, then set the original this to that copy - all inside the class declaration itself (not to an object of it in say main).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    wow, i have no idea what you just said, i think you said that the this-> isn't neded in this case, and i can just return the variables?
    If a mime dies in the woods and no one is around to hear it, does it make a sound?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If, in a class method, you have local variables with the same name as member variables, you can use this-> to specify that you want to use the member variables. Otherwise the local variables will be used. A better solution might be to just give your variables unique names.

    There are other uses of the this pointer that are slightly more advanced.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    class c {
    private:
        int i;
    public:
        c() : i(5) {}
    
        void print() {
            int i = 3;
            cout << i << endl;
            cout << this->i << endl;
        }
    };
    
    int main() {
        c var;
    
        var.print();
        return 0;
    }
    That should print
    Code:
    5
    3
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  2. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  3. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  5. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM