dimanche 12 mars 2017

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

http://ift.tt/2mBEvYQ

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

int main(int argc, char** argv)
{
    int x = 23;

    A a1(x);   // why this line is fine?

    A a2 = a1; // why this line is fine?

    a2 = a1; // error: use of deleted function 'A& A::operator=(const A&)'
            // note: 'A& A::operator=(const A&)' is implicitly deleted because the default definition would be ill-formed:
            // error: non-static reference member 'int& A::r', can't use default assignment operator
    return 0;
}

Question> The default assignment operator is deleted. Why the default copy constructor is still kept?

Aucun commentaire:

Enregistrer un commentaire