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();
}

 

运行结果:

return *this 与return this的区别

 

相关文章:

  • 2021-08-20
  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
  • 2021-11-29
  • 2022-02-12
  • 2021-12-10
猜你喜欢
  • 2021-07-22
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案