在下面的代碼中,無論我把「this->」還是刪除它都沒關係。它在兩種情況下都會提供相同的輸出和結果。 那麼,在C++中使用「this」指針有什麼意義呢?有其他用途的地方是否有必要? 謝謝。C++:是「this」指針無用嗎?
#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-else語句比那個函數中的'this->'更無用。它應該是'bool AreaSmallerThan(square c){return getArea()
Praetorian