正确答案为:return *this返回的是当前对象的克隆(当然, 这里仅考虑返回类型为A, 没有考虑返回类型为A& )。return this返回当前对象的地址(指向当前对象的指针), 下面我们来看看程序吧:
// ReturnThis.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
int x;
A(){x = 0;}//构造函数
A(const A& other)//拷贝构造函数
{
this->x = other.x;
cout << "Copy" << endl;
}
~A(){}//析构函数
A& operator=(const A& other)//赋值函数
{
this->x = other.x;
cout << "Assign" << endl;
return *this;
}
void func1(A a)
{
}
void func2(A& a)
{
}
A *func3()
{
return this;//返回当前对象的指针
}
A& func4()
{
return *this;//返回当前对象
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a1;
a1.x = 5;
A a2(a1);
A *a3 = a1.func3();
a3->x = 10;//a1.x = 10
A a4 = a1.func4();
a4.x = 10;//a4.x = 10,a1的值不变
return 0;
}