別跟我說, return *this返回當前對象, return this返回當前對象的地址(指向當前對象的指針)。 html
正確答案爲:return *this返回的是當前對象的克隆(固然, 這裏僅考慮返回類型爲A, 沒有考慮返回類型爲A& )。return this返回當前對象的地址(指向當前對象的指針), 下面咱們來看看程序吧: ios
#include <iostream> using namespace std; class A { public: int x; A* get() { return this; } }; int main() { A a; a.x = 4; if(&a == a.get()) { cout << "yes" << endl; } else { cout << "no" << endl; } return 0; }
結果爲:yes this
再看: spa
#include <iostream> using namespace std; class A { public: int x; A get() { return *this; //返回當前對象的拷貝 } }; int main() { A a; a.x = 4; if(a.x == a.get().x) { cout << a.x << endl; } else { cout << "no" << endl; } if(&a == &a.get()) { cout << "yes" << endl; } else { cout << "no" << endl; } return 0; }
結果爲: 指針
4 htm
no 對象