This is something I have wondered for a long time. Take the following example:
struct matrix
{
float data[16];
};
I know what the defau
This is what the standard says in 12.8 (Copying class objects). Copy construction:
Each subobject is copied in the manner appropriate to its type:
- if the subobject is of class type, the copy constructor for the class is used;
- if the subobject is an array, each element is copied, in the manner appropriate to the element type;
- if the subobject is of scalar type, the built-in assignment operator is used.
Copy assignment:
Each subobject is assigned in the manner appropriate to its type:
- if the subobject is of class type, the copy assignment operator for the class is used (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.
Both copies elements in the array (instead of doing nothing or copying pointer).
struct X
{
char data_[100];
};
int main ()
{
X orig, copy_assign;
orig.data_[10] = 'a';
copy_assign = orig;
X copy_constructor(orig);
printf("orginal10:%c, copy_assign10:%c, copy_constructor10:%c\n",orig.data_[10],copy_assign.data_[10],copy_constructor.data_[10]);
copy_assign.data_[10] = 'b';
printf("original10:%c, copy_assign10:%c, copy_constructor10:%c\n",orig.data_[10],copy_assign.data_[10],copy_constructor.data_[10]);
copy_constructor.data_[10] = 'c';
printf("original10:%c, copy_assign10:%c, copy_constructor10:%c\n",orig.data_[10],copy_assign.data_[10],copy_constructor.data_[10]);
return 0;
}
running results:
orginal10:a, copy_assign10:a, copy_constructor10:a
original10:a, copy_assign10:b, copy_constructor10:a
original10:a, copy_assign10:b, copy_constructor10:c
Hope this example is clear.