   a brief explanation of what this-> is???
  high,im new to programming and have been learning c++. i have a pretty decent grasp of the langage, and have worked with classes for a little bit. but i still dont understand what this-> does. could someone explain it to me? ive read a couple tutorials on it, but i just cant understand. i think im just over-complicating myself. i *think* it is just a replacement for using a period (.) but that cant be it, cuz then why would people do it? i also know it has to do with pointers of objects some how, but i dont know because i could swear ive seen it used with objects that were NOT pointers. also, some source code examples would really clear things up. maybe showing the differnce between using this-> and not using it. ive gotten some explainations from you guys that seemed to make much more sence then any tutorials, so id appreciate any help. thanks you!![edited by - graveyard filla on February 16, 2004 6:00:47 PM]
Ok, here''s the skinny:-> is used to access the members of a structure or object through a pointer.For instance:1 class foo2 {3 public:4   void go( void ) {}5   int a;6 };7 int main( int argc, char* argv[] )8 {9   foo* a_foo = new foo;10  a_foo->go();11 }The key here is this: Line 9 creates a new foo object on the heap and assigns a pointer to it to a newly created foo pointer, a_foo.Line 10 calls the foo objects go() method.Had you simply declared an instance of the foo on the stack, like so:  foo a_foo;You need only use the . operator.  a_foo.go();So, in summary, when you have a pointer to something, use ->, when you have an actual instance of something, use .    Tony;  i dont think hes talking about the -> operator i think hes taking about  this-> //this is a keyword that uses ->   [edited by - DerAngeD on February 16, 2004 6:11:13 PM];  Woohoo time to give back to these forums!Lets see if I can make this simple...Lets say we have a pointer called aPointer.  aPointer contains the memory address of a person object.  It does not, however, contain the object itself.  Now the "." operator must be used by an object, not a memory address so if you do something like this:aPointer.NameYou are trying to grab the Name attribute of aPointer, which is just an address, not an object.So what you want to do is somehow tell C++ that you want go to the address where aPointer points to (which is where the actual person object resides) and then use the "." operator.  So you could either do this:(*aPointer).Namewhich means go to that object that is pointed to by this pointer and get its Name attribute.Or you could use a shortcut:aPointer->NameThats really all the -> operator is...a shortcut so you don''t have to always write (*aPointer).Name;  As the previous poster explained, operator-> is the equivalent for operator. when operating on pointers rather than stack allocated objects. As for the keyword this, it is used in member functions of classes; the this pointer always refers to the instance of the class on which the function is invoked. It is rarely necessary (though sometimes necessary to disambiguate), but is sometimes used for clarity, necessary or not. ;  To expand, ''this'' is a pointer to the current object, and can be used within any class method. Example:class myClass { int thing; };myClass* temp = new myClass;(*temp).thing = 4;temp->thing = 4;  Those last two lines basically do the same thing. -> is sort of like the . operator, except it does the pointer dereferencing for us. And considering `this` is a pointer within the local class, we use `this->` to access members within the local class. ;  the arrow operator is often used with the this keyword to disambiguate local variables from related class member variables. Check it out: class A{private:    int foo, bar;public:    void setFoo(int foo) { foo = foo; } // doesn't work, just sets the parameter equal to itself    void setBar(int bar) { this->bar = bar; } // works, since "this->" explicitly refers to the class member}; "Sneftel is correct, if rather vulgar." --Flarelocke   [edited by - sneftel on February 16, 2004 6:18:27 PM];  I believe the original question is "why use this->".It''s true that inside class member scope, you usually don''t need "this->" because your members are magically in scope anyway. However, sometimes, it makes sense to be explicit about scope just for clarity, and sometimes you need to explicitly qualify your names because of hiding. Consider:#include <iostream>using namespace std;int x = 3;class C {  int x;public:  C() : x( 2 ) {}  void func();};void C::func(){  int x = 1;  cout << x << endl;  cout << this->x << endl;  cout << ::x << endl;}int main(){  C c;  c.func();  return 0;}  ;  As for this->...You see, a C++ method of a class is somewhat "shared" by all that classes objects.  That way, you don''t have to repeat the method''s code over and over every time you create an object.  Now because the method code is shared, how do you know what object is calling the method?  Well this is done by passing a pointer to the calling object to the method and this is done automatically for you by the compiler.  So every method which is called by an object will have a "this" pointer for you to use.  Now static methods are an exception but you will probably learn about those later.Now why would you even need this?  Well sometimes you may create a variable within your method that has the same name as an object member variable.  When this happens...how do you indicate that you want to refer to the object variable as opposed to the variable you created in the method?  Using the this pointer of course =) like so....this->objectMemberVariable;  This example of this   may be a bit complicated, but it is a very important one, that I just used today, in fact.  (It deals with Win32 threads, by the way, but might apply to a variety of situations.)  If it''s too complex, just ignore it, but maybe someone might learn something from it...Say you have a class that creates a thread in its constructor.  The Win32 function CreateThread() supports a void* parameter that will be passed to the thread function, when it starts.  The thread function, however, has to be a static function, it can''t be a member function of a class.  And a static function can''t know what object it is associated with.  So you pass this   to CreateThread(), and then the static thread function has a pointer to an actual object.  Here''s sort of what I wrote today: class CRenderer{  public:    CRenderer()    { mhThread = CreateThread(NULL, 0, CRenderer::ThreadEntry_Static, this, 0, NULL);    }  protected:    static DWORD WINAPI ThreadEntry_Static(VOID* pParameter)    { return ((CRenderer*)pParameter)->ThreadEntry();    }    DWORD ThreadEntry()    { //Do Thread Stuff return 0;    }    HANDLE mhThread;};So in the constructor, CreateThread() is told that the thread function is CRenderer::ThreadEntry_Static, and that the parameter is this  .  So it creates a thread, and begins running ThreadEntry_Static(), whose parameter pParameter is equal to this  , which is the pointer to the object that created the thread.  It casts the VOID* to a CRenderer*, and calls the member function ThreadEntry().  At that point, everything runs like normal.