// this
#include <iostream>
usingnamespace std;
class CDummy {
public:
int isitme (CDummy& param);
};
int CDummy::isitme (CDummy& param)
{
if (¶m == this) returntrue;
elsereturnfalse;
}
int main () {
CDummy a;
CDummy* b = &a;
if ( b->isitme(a) )
cout << "yes, &a is b";
return 0;
}
So basically, in this case, this is just a pointer to a CDummy object? And if (¶m == this) simply checks if ¶m is a pointer to a CDummy object?
Not just that, it checks to see if param is the same as the object that is calling the function. The class on the left side of the method becomes the "this" pointer inside the method.
Uh.. so, if this is in a function call, it would mean a pointer to the object that calls the function? So it's the object calling the function that determines the nature of this?
Or is it the fact that isitme is defined within the CDummy class?
this is passed into all instance methods automatically as the first parameter (hidden in the method declarations).
someVar->someInstanceMethod();
this will equal someVar inside someInstanceMethod
The other type of methods in a class are static "class" methods, these do not have a this pointer, they are not called on an instance of the class, called as someClass::someStaticMethod()