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; }
if -elsestatement is even more useless than thethis->in that function. It should bebool AreaSmallerThan(square c) { return getArea() < c.getArea(); }You may also want to change that parameter tosquare const& cto avoid an unnecessary copy.