Reputation: 924
in the code below, it does not matter whether i put "this->" or remove it. it gives same output and result in both cases. So, what is the point of having the "this" pointer in C++? Are there other usages where it is essential? Thanks.
#include<iostream>
using namespace std;
class square{
int l;
int w;
public:
square(int x, int y){
w = x;
l = y;
}
int getArea(){
return w * l;
};
bool AreaSmallerThan(square c){
if(this->getArea() < c.getArea())
return true;
else
return false;
}
};
int main(){
square A(2,3);
square B(1,3);
if(A.AreaSmallerThan(B))
cout<<"A is smaller than B."<<endl;
else
cout<<"A is NOT smaller than B."<<endl;
return 0;
}
Upvotes: 2
Views: 857
Reputation: 146
Generally speaking, the 'this' pointer can be useful when an instance of a class is invoking some method and the instance itself needs to be passed from within the method to some function outside of the class.
Upvotes: 0
Reputation: 283684
Yes there are times it is essential. A classic case is in operator=
, to avoid destroying resources during self-assignment.
for example https://stackoverflow.com/a/3975092/103167
Set& Set::operator=(const Set& setEqual)
{
//first check for self assignment
if (&setEqual == this)
cout << "this is self assignment";
return *this;
}
(Note that this isn't needed when using the copy-and-swap idiom)
Accessing members via this
is also frequently seen in template code which inherits from a template parameter. Those inherited names can only be found during second phase lookup, which means they need to be qualified with this->
Upvotes: 7
Reputation: 24249
TL;DR: It has it's uses. If you choose good naming practices, you generally won't need to use it often.
There are a number of cases where you would want a "pointer to the current object", for example:
struct Foo
{
void MakeCallback(eventid_t eventId)
{
scheduleCallback(eventId, callbackProxyFn, this);
}
static void callbackProxyFn(eventid_t eventId, Foo* foo)
{
// call 'callback' on the relevant object instance.
foo->callback(eventId);
}
void callback(eventid_t eventId);
};
It can also be used to resolve conflicts between names in the current object and other scopes, if you choose to use terrible naming conventions.
void Foo::bar(int n)
{
this->n = n;
}
You could avoid this (pun intended) scenario, as is common practice, by prefixing statics, globals and members:
class Player {
int m_score;
public:
Player(int score) : m_score(score) {}
};
Player g_player1;
static Player s_login; // yeah, I know, terrible, just an example tho.
A common use is in eliminating self in copy/comparison operators:
bool Foo::operator==(const Foo& rhs) const
{
if (this == &rhs)
return true;
...
}
You can also use it to produce a reference to the current object:
foo(const Foo&);
void foo(*this);
Upvotes: 7
Reputation: 2646
Random example...what if you had passed an int w, int l
into your getArea()
function...then you need to use the this
keyword to differentiate between the local parameter
int getArea(int w, int l){
return this->w * this->l;
};
Another common example might be move assignment. I have pasted an example from a Tree datastructure project I coded.
/* move assignment */
TreeSet& operator= (TreeSet&& rhs)
{
clearAll(rootPtr);
this->rootPtr = rhs.rootPtr;
rhs.rootPtr = nullptr;
return *this;
}
And finally another example for an iterator I wrote...when overloading the ++ operator on an iterator, you want to return the resulting iterator..
/* Update the current pointer to advance to the node
* with the next larger value
*/
const_iterator& operator++ () {
//I have deleted all the logic for the sake of not taking up a ton of space..
return *this;
}
Upvotes: 6