c++ default assignment operator

int a[10];
int b[10];
a = b;

//

struct test {
    int a[10];
};

test a,b;
a = b;

First code doesn't compile, since we cant assign array, but the second does. Isn't the default assignment operator of class simply call assignment for each data members? Why does the the second code compile?


From the C++11 draft, section 12.8:

The implicitly-defined copy/move assignment operator for a non-union class X performs memberwise copy/move assignment of its subobjects. The direct base classes of X are assigned first, in the order of their declaration in the base-specifier-list, and then the immediate non-static data members of X are assigned, in the order in which they were declared in the class definition. Let x be either the parameter of the function or, for the move operator, an xvalue referring to the parameter. Each subobject is assigned in the manner appropriate to its type:

— if the subobject is of class type, as if by a call to operator= with the subobject as the object expression and the corresponding subobject of x as a single function argument (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);

— if the subobject is an array, each element is assigned, in the manner appropriate to the element type;

— if the subobject is of scalar type, the built-in assignment operator is used.

The important part here is: if the subobject is an array, each element is assigned, in the manner appropriate to the element type;

链接地址: http://www.djcxy.com/p/73248.html

上一篇: Git不能差异或合并UTF文件中的.cs文件

下一篇: c ++默认赋值运算符