#include "stdio.h"
class Object{
public:
  int i;
  Object& method1(){
    return *this;
  }
};

int main(int argc, char const *argv[]) {
  Object o;
  Object o1=o.method1();
  Object& o2=o.method1();
  o1.i=0;
  printf("%d\n",o.i);
  o2.i=1;
  printf("%d\n",o.i);
  return 0;
}

结果

-1589001648
1

可以看到,当即使返回是引用,但是如果赋值对象没有采用引用也不会影响到原来的对象。

当然,如果返回不是引用却赋值给引用对象,就会编译错误。

 

未经许可,不允许转载

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-28
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-05
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案