Are there any uses for "this" besides cascading?
Are there any uses for "this" besides cascading?
'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition
there are lots of uses for lots of different pointers. "this" is used so that you can internally figure out what your own pointer is. In COM you have a function called QueryInterface where there is a cast of the this pointer. looks something like this
this is not exactly how they do it. They actually use static_cast but you get the idea. It's to get a pointer to a specific base class inside a void *. (in case you didn't know, base class pointers are actually a different address than the actual address)Code:ptrtoreturn = (void*)(InterfaceName*)this;
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
sure! As long as you understand what this is, you'll use it for all sorts of stuffmight be used in a linked list, for example.Code:if (!this->Head) {...}
or another example: if a member function returns a reference or pointer to its own object.
ie:
return *this;
or
return this;