这个成语是什么,什么时候应该使用?它解决了哪些问题?当使用C++ 11时,成语是否会改变?
虽然在许多地方已经提到过,但我们没有任何单一的"它是什么"问题和答案,所以在这里.以下是前面提到的地方的部分列表:
c++ c++-faq copy-constructor assignment-operator copy-and-swap
在下面的代码中,我是否放置"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;
}
Run Code Online (Sandbox Code Playgroud)