admin管理员组文章数量:1321245
Suppose this class in C++
class Message {
char msg[64];
};
Why are operator=
and default copy already correct
Message & operator=(const Message &o) {
if (this != &o)
this->msg = o.msg;
}
return *this;
}
In assignment should be wrong?
Isn't it the default semantics to use assignment operator field per field to implement default assignment and copy construction
Suppose this class in C++
class Message {
char msg[64];
};
Why are operator=
and default copy already correct
Message & operator=(const Message &o) {
if (this != &o)
this->msg = o.msg;
}
return *this;
}
In assignment should be wrong?
Isn't it the default semantics to use assignment operator field per field to implement default assignment and copy construction
Share Improve this question edited Jan 17 at 17:18 Yann TM asked Jan 17 at 15:04 Yann TMYann TM 2,07515 silver badges23 bronze badges 11 | Show 6 more comments1 Answer
Reset to default 9The implicitly-defined copy assignment operator is not as simple as "apply assignment to each member". Arrays are specifically taken into account as a special case.
[class.copy.assign]/12 The implicitly-defined copy/move assignment operator for a non-union class
X
performs memberwise copy/move assignment of its subobjects... Each subobject is assigned in the manner appropriate to its type:
...
(12.2) if the subobject is an array, each element is assigned, in the manner appropriate to the element type;
...
本文标签: ccopy and assignment operator with constant size arraysStack Overflow
版权声明:本文标题:c++ - copy and assignment operator with constant size arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742097410a2420634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
o.mg
– Louis Go Commented Jan 17 at 15:07memcpy
. In any case, the member that's an array of objects that are not trivially copyable works just as well. – Igor Tandetnik Commented Jan 17 at 15:25