cpp/language/copy assignment: Difference between revisions
From cppreference.com
m →Syntax: Drop whitesapce from {{ttb}}. |
m →Notes: shorter |
||
| (47 intermediate revisions by 14 users not shown) | |||
| Line 2: | Line 2: | ||
{{cpp/language/classes/navbar}} | {{cpp/language/classes/navbar}} | ||
A copy assignment operator | A copy assignment operator is a non-template {{rlp|member functions|non-static member function}} with the name {{c|operator=}} that of type . | ||
===Syntax=== | ===Syntax=== | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc |num=1| {{spar| | {{sdsc|num=1|{{spar|}} {{ttb|}}{{spar |}}{{ttb| | ||
{{sdsc |num= | }} {{ttb|operator{{}}}} {{spar|}}}} | ||
{{ | {{sdsc|num=|{{spar|}} {{ttb|}}{{spar |}}{{ttb|}}|{{}}}} | ||
{{sdsc |num= | {{spar|}} {{|}}}}{{|= |notes={{mark since c++11}} | ||
{{|{{|}} {{spar |}}{{ttb|::}}{{ttb|operator=}}{{spar |}}{{|)}} {{|}}}} | |||
{{sdsc|num=|{{spar|}} {{spar |}}{{ttb|::}}{{ttb|operator=}}{{spar |}}{{|) = }}{{c}}}} | |||
{{sdsc end}} | {{sdsc end}} | ||
===Explanation=== | ===Explanation=== | ||
of a copy assignment operator . | |||
of a copy assignment operator | |||
copy - | |||
. | |||
a copy assignment operator the . | |||
copy assignment . | |||
The copy assignment operator is called whenever selected by | |||
The copy assignment operator is called whenever selected by |overload resolution, e.g. when an object appears on the left side of an assignment expression. | |||
===Implicitly-declared copy assignment operator=== | ===Implicitly-declared copy assignment operator=== | ||
If no user-defined copy assignment operators are provided for a class type | If no user-defined copy assignment operators are provided for a class type, the compiler will always declare one as an inline publicmember of the class. This implicitly-declared copy assignment operator has the form {{c|1=T& T::operator=(const T&)}} if all of the following is true: | ||
* each direct base {{tt|B}} of {{tt|T}} has a copy assignment operator whose parameters are {{ | * each direct base {{tt|B}} of {{tt|T}} has a copy assignment operator whose parameters are {{|B}} or {{c|const B&}} or {{c|const volatile B&}}; | ||
* each non-static data member {{tt|M}} of {{tt|T}} of class type or array of class type has a copy assignment operator whose parameters are {{ | * each non-static data member {{tt|M}} of {{tt|T}} of class type or array of class type has a copy assignment operator whose parameters are {{|M}} or {{c|const M&}} or {{c|const volatile M&}}. | ||
Otherwise the implicitly-declared copy assignment operator is declared as {{c|1=T& T::operator=(T&)}}. | Otherwise the implicitly-declared copy assignment operator is declared as {{c|1=T& T::operator=(T&)}}. | ||
, the implicitlydeclared copy assignment operator {{|}} . | |||
The implicitly-declared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in {{rev inl|until=c++17|{{rlp| | |||
The implicitly-declared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in {{rev inl|until=c++17|{{rlp||dynamic exception specification}}}}{{rev inl|since=c++17|{{rlp||specification}}}} | |||
Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a {{rlp|using declaration#In class definition|using-declaration}} is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration. | Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a {{rlp|using declaration#In class definition|using-declaration}} is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration. | ||
=== | === | ||
implicitly-declared copy assignment operator = | |||
* {{tt|T}} | |||
== | |||
implicitly-copy assignment operator for class {{tt|T}} is if | |||
the is | |||
* {{tt|T}} a | |||
- | |||
{{tt|T}} has a user-declared . | |||
* {{tt|T}} has a non-static data member of non-class type (or array thereof) | defaultedcopy assignment operator for class {{tt|T}} is defined as deletedif any of the following is : | ||
* {{tt|T}} has a non-static data member of a reference type | * {{tt|T}} has a non-static data member of non-class type (or array thereof) | ||
* {{tt|T}} has a | * {{tt|T}} has a non-static data member of a reference type | ||
* {{ | * {{tt|T}} has a -that overload resolution copy assignment | ||
, or | |||
* {{|union#Union-like classes| | |||
-class a assignment operator. | |||
===Trivial copy assignment operator=== | ===Trivial copy assignment operator=== | ||
The copy assignment operator for class {{tt|T}} is trivial if all of the following is true: | The copy assignment operator for class {{tt|T}} is trivial if all of the following is true: | ||
* it is not user-provided (meaning, it is implicitly-defined or defaulted) | * it is not user-provided (meaning, it is implicitly-defined or defaulted); | ||
* {{tt|T}} has no virtual member functions; | * {{tt|T}} has no virtual member functions; | ||
* {{tt|T}} has no virtual base classes; | * {{tt|T}} has no virtual base classes; | ||
* the copy assignment operator selected for every direct base of {{tt|T}} is trivial; | * the copy assignment operator selected for every direct base of {{tt|T}} is trivial; | ||
* the copy assignment operator selected for every non-static class type (or array of class type) member of {{tt|T}} is trivial | * the copy assignment operator selected for every non-static class type (or array of class type) member of {{tt|T}} is trivial. | ||
A trivial copy assignment operator makes a copy of the object representation as if by {{lc|std::memmove}}. All data types compatible with the C language (POD types) are trivially copy-assignable. | A trivial copy assignment operator makes a copy of the object representation as if by {{lc|std::memmove}}. All data types compatible with the C language (POD types) are trivially copy-assignable. | ||
=== | ===copy assignment operator=== | ||
{{|copy assignment }} | |||
of copy assignment is | | |||