why default copy-ctor is generated for a class with reference member variable?

后端 未结 3 1357
予麋鹿
予麋鹿 2021-01-05 02:41

http://coliru.stacked-crooked.com/a/8356f09dff0c9308

#include 
struct A
{
  A(int& var) : r(var) {}
  int &r;
};

int main(int argc,          


        
3条回答
  •  执笔经年
    2021-01-05 03:29

    Assignment and construction are two different beasts.

    The default assignment operator cannot exist because references cannot be assigned to (if you try in "user-land" code, you will actually be assigning to the referant).

    The default copy constructor has no such problem, since we can create references just fine.

提交回复
热议问题