Nothing personal to *this or anything, but why exactly would you need to use this? I mean, if there's a member function, couldn't you just use the functions name? What advantage does this have?
Nothing personal to *this or anything, but why exactly would you need to use this? I mean, if there's a member function, couldn't you just use the functions name? What advantage does this have?
wow, that's not very well thought out. "this" gives you the ability to tell someone else where you are. Imagine you are adding yourself to a list, you'ld use your pointer. Imagine you are giving a child object a "callback" pointer. You would give it your pointer. That is what "this" is for and not just to call your own member functions.
oh and...
you->suck
Well, this is from more of a Java perspective, but what I use 'this' for mostly is to call overloaded/overriddden constructors or base class constructors.
I've got too many languages in my head, hard to keep them straight sometimes. For example, in C/C++, a semi-colon marks the end of a statement. In Dataflex/XBase, it means the statement continues on the next line. Aaaarrrgghh!
Truth is a malleable commodity - Dick Cheney
this enables you to return a reference or pointer to the current object. Other than that, I don't believe it fundamentally enables things that couldn't be done without it.
"this" is important because not everything should be done from outside an object.
Example one....
In windows programming, if you have a class that represents a window the on create might look something like this...
Then your callback will use the pointer to access the object You can use SetWindowLong to stick the pointer permanently in GWL_USERDATACode:CreateWindow(blah blah blah, this); // This puts the pointer to you in the window callback
Example two.
if you want creation of an object to store that instance in a global list for instance. The only way for a constructor to put himself in a list is to have access to his own pointer. Something like this:
It's pretty simple actually. "this" is necessary.Code:ClassName::ClassName() { g_list.Add(this); } ClassName::~ClassName() { g_list.Remove(this); }