What does & = in C& operator=(const C&) & = default; do? [duplicate]

随声附和 提交于 2019-12-24 00:07:22

问题


A few questions on SO use a particular syntax for declaring default assignment operators.

Rule-of-Three becomes Rule-of-Five with C++11?

class C {
  C(const C&) = default;
  C(C&&) = default;
  C& operator=(const C&) & = default;
  C& operator=(C&&) & = default;
  virtual ~C() { }
};

I'm confused by the & = used for the assignment operators. After a quick test, default assignment operator declarations seem to compile and give the expected behavior with or without the additional ampersand.

I don't see the & = syntax on cppreference.


回答1:


The & there is a ref qualifier.

In that particular case it makes it so that the instance of C you want to assign to must be a non-const lvalue.



来源:https://stackoverflow.com/questions/24484365/what-does-in-c-operator-const-c-default-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!