return *this 与return this的区别

return *this返回当前对象的引用(也就是返回当前对象)

return this返回当前对象的地址.

#include <iostream>
using namespace std;

class A
{
public:
    int x;
    A* get1()
    {
        return this;
    }
    A& get2()
    {
        return *this;
    }
};
int main(void)
{
    A a;
    cout<<"return this返回:";
    cout<<a.get1()<<endl;
    cout<<"return *this返回:";
    cout<<&a.get2()<<endl;//return *this 返回的是对象,需要取地址符号才能将其打印出来
    getchar();
}

 

运行结果:

 

posted @ 2014-06-22 10:15  北门吹风  阅读(723)  评论(0编辑  收藏  举报