- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
- OverflowAI GenAI features for Teams
- OverflowAPI Train & fine-tune LLMs
- Labs The future of collective knowledge sharing
- About the company Visit the blog
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
When to delete copy constructor and assignment operator? [duplicate]
I am often seeing such (simplified) code:
Then, throughout the program, the class A is instantiated only once.
What are the practical reaons to delete a copy constructor and operator= and at the same time not make the class Singleton?
- constructor
- 1 Does this answer your question? Why and when delete copy constructor and operator= or When to delete copy constructor – walnut Commented Dec 28, 2019 at 20:24
- 3 Consider std::thread , which represents a single thread. There can certainly be multiple threads, but what would be the semantics of copying a thread? – walnut Commented Dec 28, 2019 at 20:26
2 Answers 2
If you have a class that have vars allocating dynamic memory and you don't want to make a suitable copy ctor and cpy assignment operator to allocate a new memory to avoide having two pointers pointing to the same thing, you can delete the whole cpy ctor and cpy assignment operator.
If you want to disallow passing the object by value, you may delete them too.
When to delete copy constructor and assignment operator?
Copy constructor (and assignment) should be defined when ever the implicitly generated one violates any class invariant.
It should be defined as deleted when it cannot be written in a way that wouldn't have undesirable or surprising behaviour.
Probably the simplest example is the class std::unique_ptr . As the name implies, it has unique ownership of a pointer value. Copying the internal pointer would violate the invariant of unique ownership. That's what the implicit copy constructor (and assignment) would do. But there is no reasonable alternative either. Maybe you could implement a copy constructor that dynamically allocates a new object, and copy-initialises it from the originally pointed object. But then this pointer would have different behaviour from normal pointers whose copy will not cause allocation or creation of new pointed objects, which may be surprising and undesirable. Furthermore, std::unique_ptr needs to also be usable with pointed types that themselves might not be copyable.
Not the answer you're looking for? Browse other questions tagged c++ oop constructor copy singleton or ask your own question .
- The Overflow Blog
- Battling ticket bots and untangling taxes at the frontiers of e-commerce
- Ryan Dahl explains why Deno had to evolve with version 2.0
- Featured on Meta
- We've made changes to our Terms of Service & Privacy Policy - July 2024
- Bringing clarity to status tag usage on meta sites
- Feedback requested: How do you use tag hover descriptions for curating and do...
Hot Network Questions
- Why would an incumbent politician or party need to be re-elected to fulfill a campaign promise?
- Ecuador: what not to take into the rainforest due to humidity?
- Small debris which has punctured a tubeless tire: to leave or to remove?
- What is the origin of this quote on telling a big lie?
- Electric moped PWM MOSFETs failure
- A possibly Varley short fiction about a clone, his sister-progenitor and a complicated family history
- How can flyby missions work?
- Could a gas giant still be low on the horizon from the equator of a tidally locked moon?
- Is it possible to create your own toy DNS root zone?
- Trigger (after delete) restricts from deleting records
- Has any spacecraft ever been severely damaged by a micrometeriote?
- VerificationTest leaks message?
- Operator-sum representation of the two-qubit depolarizing channel
- Reference Request: Preservation of étale maps under rigid analytic GAGA
- Creating a deadly "minimum altitude limit" in an airship setting
- How many people could we get off of the planet in a month?
- Why are swimming goggles typically made from a different material than diving masks?
- Is the Garmin Edge 530 still a good choice for a beginner in 2024?
- Fitting the 9th piece into the pizza box
- Why is Ganondorf in LOZ Windwaker?
- How to make outhouses less icky?
- Can science inform philosophy?
- How common is it for external contractors to manage internal teams, and how can we navigate this situation?
- What to do if sample size obtained is much larger than indicated in the power analysis?
Home » Learn C Programming from Scratch » C Assignment Operators
C Assignment Operators
Summary : in this tutorial, you’ll learn about the C assignment operators and how to use them effectively.
Introduction to the C assignment operators
An assignment operator assigns the vale of the right-hand operand to the left-hand operand. The following example uses the assignment operator (=) to assign 1 to the counter variable:
After the assignmment, the counter variable holds the number 1.
The following example adds 1 to the counter and assign the result to the counter:
The = assignment operator is called a simple assignment operator. It assigns the value of the left operand to the right operand.
Besides the simple assignment operator, C supports compound assignment operators. A compound assignment operator performs the operation specified by the additional operator and then assigns the result to the left operand.
The following example uses a compound-assignment operator (+=):
The expression:
is equivalent to the following expression:
The following table illustrates the compound-assignment operators in C:
Operator | Operation Performed | Example | Equivalent expression |
---|---|---|---|
Multiplication assignment | x *= y | x = x * y | |
Division assignment | x /= y | x = x / y | |
Remainder assignment | x %= y | x = x % y | |
Addition assignment | x += y | x = x + y | |
Subtraction assignment | x -= y | x = x – y | |
Left-shift assignment | x <<= y | x = x <<=y | |
Right-shift assignment | x >>=y | x = x >>= y | |
Bitwise-AND assignment | x &= y | x = x & y | |
Bitwise-exclusive-OR assignment | x ^= y | x = x ^ y | |
Bitwise-inclusive-OR assignment | x |= y | x = x | y |
- A simple assignment operator assigns the value of the left operand to the right operand.
- A compound assignment operator performs the operation specified by the additional operator and then assigns the result to the left operand.
Copy assignment operator
(C++11) | ||||
(C++11) | ||||
(C++11) |
General topics | ||||
statement |
loop | ||||
loop (C++11) |
loop | ||||
loop |
statement | ||||
statement |
(C++11) | ||||
Literals | ||||
(C++11) | ||||
(C++11) |
expression |
pointer | ||||
(C++11) | ||||
(C++11) |
(C++11) | ||||
A copy assignment operator of class T is a non-template non-static member function with the name operator = that takes exactly one parameter of type T , T & , const T & , volatile T & , or const volatile T & . A type with a public copy assignment operator is CopyAssignable .
Syntax Explanation Implicitly-declared copy assignment operator Deleted implicitly-declared copy assignment operator Trivial copy assignment operator Implicitly-defined copy assignment operator Notes Copy and swap Example |
[ edit ] Syntax
class_name class_name ( class_name ) | (1) | (since C++11) | |||||||
class_name class_name ( const class_name ) | (2) | (since C++11) | |||||||
class_name class_name ( const class_name ) = default; | (3) | (since C++11) | |||||||
class_name class_name ( const class_name ) = delete; | (4) | (since C++11) | |||||||
[ edit ] Explanation
- Typical declaration of a copy assignment operator when copy-and-swap idiom can be used
- Typical declaration of a copy assignment operator when copy-and-swap idiom cannot be used
- Forcing a copy assignment operator to be generated by the compiler
- Avoiding implicit copy assignment
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.
[ edit ] Implicitly-declared copy assignment operator
If no user-defined copy assignment operators are provided for a class type ( struct , class , or union ), the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form T & T :: operator = ( const T & ) if all of the following is true:
- each direct base B of T has a copy assignment operator whose parameters are B or const B& or const volatile B &
- each non-static data member M of T of class type or array of class type has a copy assignment operator whose parameters are M or const M& or const volatile M &
Otherwise the implicitly-declared copy assignment operator is declared as T & T :: operator = ( T & ) . (Note that due to these rules, the implicitly-declared copy assignment operator cannot bind to a volatile lvalue argument)
A class can have multiple copy assignment operators, e.g. both T & T :: operator = ( const T & ) and T & T :: operator = ( T ) . If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default .
Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a 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.
[ edit ] Deleted implicitly-declared copy assignment operator
The implicitly-declared or defaulted copy assignment operator for class T is defined as deleted in any of the following is true:
- T has a non-static data member that is const
- T has a non-static data member of a reference type.
- T has a non-static data member that cannot be copy-assigned (has deleted, inaccessible, or ambiguous copy assignment operator)
- T has direct or virtual base class that cannot be copy-assigned (has deleted, inaccessible, or ambiguous move assignment operator)
- T has a user-declared move constructor
- T has a user-declared move assignment operator
[ edit ] Trivial copy assignment operator
The implicitly-declared copy assignment operator for class T is trivial if all of the following is true:
- T has no virtual member functions
- T has no virtual base classes
- The copy assignment operator selected for every direct base of T is trivial
- The copy assignment operator selected for every non-static class type (or array of class type) memeber of T is trivial
A trivial copy assignment operator makes a copy of the object representation as if by std:: memmove . All data types compatible with the C language (POD types) are trivially copy-assignable.
[ edit ] Implicitly-defined copy assignment operator
If the implicitly-declared copy assignment operator is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined copy assignment copies the object representation (as by std:: memmove ). For non-union class types ( class and struct ), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using, using built-in assignment for the scalars and copy assignment operator for class types.
The generation of the implicitly-defined copy assignment operator is deprecated (since C++11) if T has a user-declared destructor or user-declared copy constructor.
[ edit ] Notes
If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is an rvalue (either prvalue such as a nameless temporary or xvalue such as the result of std:: move ), and selects the copy assignment if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.
[ edit ] Copy and swap
Copy assignment operator can be expressed in terms of copy constructor, destructor, and the swap() member function, if one is provided:
T & T :: operator = ( T arg ) { // copy/move constructor is called to construct arg swap ( arg ) ; // resources exchanged between *this and arg return * this ; } // destructor is called to release the resources formerly held by *this
For non-throwing swap(), this form provides strong exception guarantee . For rvalue arguments, this form automatically invokes the move constructor, and is sometimes referred to as "unifying assignment operator" (as in, both copy and move).
[ edit ] Example
01 Career Opportunities
02 beginner, 03 intermediate, 04 advanced, 05 training programs, c programming assignment operators, free c programming online course with certificate, what is an assignment operator in c, types of assignment operators in c.
1. Simple Assignment Operator (=)
Example of simple assignment operator.
2. Compound Assignment Operators
+= | addition assignment | It adds the right operand to the left operand and assigns the result to the left operand. |
-= | subtraction assignment | It subtracts the right operand from the left operand and assigns the result to the left operand. |
*= | multiplication assignment | It multiplies the right operand with the left operand and assigns the result to the left operand |
/= | division assignment | It divides the left operand with the right operand and assigns the result to the left operand. |
%= | modulo assignment | It takes modulus using two operands and assigns the result to the left operand. |
Example of Augmented Arithmetic and Assignment Operators
&= | bitwise AND assignment | It performs the bitwise AND operation on the variable with the value on the right |
|= | bitwise OR assignment | It performs the bitwise OR operation on the variable with the value on the right |
^= | bitwise XOR assignment | It performs the bitwise XOR operation on the variable with the value on the right |
<<= | bitwise left shift assignment | Shifts the bits of the variable to the left by the value on the right |
>>= | bitwise right shift assignment | Shifts the bits of the variable to the right by the value on the right |
Example of Augmented Bitwise and Assignment Operators
Practice problems on assignment operators in c, 1. what will the value of "x" be after the execution of the following code, 2. after executing the following code, what is the value of the number variable, benefits of using assignment operators, best practices and tips for using the assignment operator, live classes schedule.
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast | |
Filling Fast |
About Author
- Sign In / Suggest an Article
Current ISO C++ status
Upcoming ISO C++ meetings
Upcoming C++ conferences
Compiler conformance status
ISO C++ committee meeting
June 24-29, St. Louis, MO, USA
July 2-5, Folkestone, Kent, UK
Quick Q: Most concise way to disable copy and move semantics
By Adrien Hamelin | Feb 20, 2018 08:40 AM | Tags: c++11 advanced
Quick A: Delete the move assignment.
Recently on SO:
Most concise way to disable copy and move semantics According to this chart (by Howard Hinnant): The most concise way is to =delete move assignment operator (or move constructor, but it can cause problems mentioned in comments). Though, in my opinion the most readable way is to =delete both copy constructor and copy assignment operator.
Share this Article
Add a comment, comments (0).
There are currently no comments on this entry.
cppreference.com
Copy assignment operator.
(C++20) | ||||
(C++20) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++17) | ||||
General | ||||
Members | ||||
pointer | ||||
(C++11) | ||||
specifier | ||||
specifier | ||||
Special member functions | ||||
(C++11) | ||||
(C++11) | ||||
Inheritance | ||||
(C++11) | ||||
(C++11) |
A copy assignment operator of class T is a non-template non-static member function with the name operator = that takes exactly one parameter of type T , T & , const T & , volatile T & , or const volatile T & . For a type to be CopyAssignable , it must have a public copy assignment operator.
Syntax Explanation Implicitly-declared copy assignment operator Deleted implicitly-declared copy assignment operator Trivial copy assignment operator Implicitly-defined copy assignment operator Notes Example Defect reports |
[ edit ] Syntax
class_name class_name ( class_name ) | (1) | ||||||||
class_name class_name ( const class_name ) | (2) | ||||||||
class_name class_name ( const class_name ) = default; | (3) | (since C++11) | |||||||
class_name class_name ( const class_name ) = delete; | (4) | (since C++11) | |||||||
[ edit ] Explanation
- Typical declaration of a copy assignment operator when copy-and-swap idiom can be used.
- Typical declaration of a copy assignment operator when copy-and-swap idiom cannot be used (non-swappable type or degraded performance).
- Forcing a copy assignment operator to be generated by the compiler.
- Avoiding implicit copy assignment.
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.
[ edit ] Implicitly-declared copy assignment operator
If no user-defined copy assignment operators are provided for a class type ( struct , class , or union ), the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form T & T :: operator = ( const T & ) if all of the following is true:
- each direct base B of T has a copy assignment operator whose parameters are B or const B & or const volatile B & ;
- each non-static data member M of T of class type or array of class type has a copy assignment operator whose parameters are M or const M & or const volatile M & .
Otherwise the implicitly-declared copy assignment operator is declared as T & T :: operator = ( T & ) . (Note that due to these rules, the implicitly-declared copy assignment operator cannot bind to a volatile lvalue argument.)
A class can have multiple copy assignment operators, e.g. both T & T :: operator = ( const T & ) and T & T :: operator = ( T ) . If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default . (since C++11)
The implicitly-declared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in dynamic exception specification (until C++17) exception specification (since C++17)
Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a 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.
[ edit ] Deleted implicitly-declared copy assignment operator
A implicitly-declared copy assignment operator for class T is defined as deleted if any of the following is true:
- T has a user-declared move constructor;
- T has a user-declared move assignment operator.
Otherwise, it is defined as defaulted.
A defaulted copy assignment operator for class T is defined as deleted if any of the following is true:
- T has a non-static data member of non-class type (or array thereof) that is const ;
- T has a non-static data member of a reference type;
- T has a non-static data member or a direct or virtual base class that cannot be copy-assigned (overload resolution for the copy assignment fails, or selects a deleted or inaccessible function);
- T is a union-like class , and has a variant member whose corresponding assignment operator is non-trivial.
[ edit ] Trivial copy assignment operator
The copy assignment operator for class T is trivial if all of the following is true:
- it is not user-provided (meaning, it is implicitly-defined or defaulted) , , and if it is defaulted, its signature is the same as implicitly-defined (until C++14) ;
- T has no virtual member functions;
- T has no virtual base classes;
- the copy assignment operator selected for every direct base of T is trivial;
- the copy assignment operator selected for every non-static class type (or array of class type) member of T is trivial;
has no non-static data members of -qualified type. | (since C++14) |
A trivial copy assignment operator makes a copy of the object representation as if by std::memmove . All data types compatible with the C language (POD types) are trivially copy-assignable.
[ edit ] Implicitly-defined copy assignment operator
If the implicitly-declared copy assignment operator is neither deleted nor trivial, it is defined (that is, a function body is generated and compiled) by the compiler if odr-used . For union types, the implicitly-defined copy assignment copies the object representation (as by std::memmove ). For non-union class types ( class and struct ), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using built-in assignment for the scalars and copy assignment operator for class types.
The generation of the implicitly-defined copy assignment operator is deprecated (since C++11) if T has a user-declared destructor or user-declared copy constructor.
[ edit ] Notes
If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is an rvalue (either a prvalue such as a nameless temporary or an xvalue such as the result of std::move ), and selects the copy assignment if the argument is an lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.
It is unspecified whether virtual base class subobjects that are accessible through more than one path in the inheritance lattice, are assigned more than once by the implicitly-defined copy assignment operator (same applies to move assignment ).
See assignment operator overloading for additional detail on the expected behavior of a user-defined copy-assignment operator.
[ edit ] Example
[ edit ] defect reports.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
C++14 | operator=(X&) = default was non-trivial | made trivial |
- Pages with unreviewed CWG DR marker
- Recent changes
- Offline version
- What links here
- Related changes
- Upload file
- Special pages
- Printable version
- Permanent link
- Page information
- In other languages
- This page was last modified on 9 January 2019, at 07:16.
- This page has been accessed 570,566 times.
- Privacy policy
- About cppreference.com
- Disclaimers
21.12 — Overloading the assignment operator
- Graphics and multimedia
- Language Features
- Unix/Linux programming
- Source Code
- Standard Library
- Tips and Tricks
- Tools and Libraries
- Windows API
- Copy constructors, assignment operators,
Copy constructors, assignment operators, and exception safe assignment
MyClass& other ); MyClass( MyClass& other ); MyClass( MyClass& other ); MyClass( MyClass& other ); |
MyClass* other ); |
MyClass { x; c; std::string s; }; |
MyClass& other ) : x( other.x ), c( other.c ), s( other.s ) {} |
); |
print_me_bad( std::string& s ) { std::cout << s << std::endl; } print_me_good( std::string& s ) { std::cout << s << std::endl; } std::string hello( ); print_me_bad( hello ); print_me_bad( std::string( ) ); print_me_bad( ); print_me_good( hello ); print_me_good( std::string( ) ); print_me_good( ); |
, ); |
=( MyClass& other ) { x = other.x; c = other.c; s = other.s; * ; } |
< T > MyArray { size_t numElements; T* pElements; : size_t count() { numElements; } MyArray& =( MyArray& rhs ); }; |
<> MyArray<T>:: =( MyArray& rhs ) { ( != &rhs ) { [] pElements; pElements = T[ rhs.numElements ]; ( size_t i = 0; i < rhs.numElements; ++i ) pElements[ i ] = rhs.pElements[ i ]; numElements = rhs.numElements; } * ; } |
<> MyArray<T>:: =( MyArray& rhs ) { MyArray tmp( rhs ); std::swap( numElements, tmp.numElements ); std::swap( pElements, tmp.pElements ); * ; } |
< T > swap( T& one, T& two ) { T tmp( one ); one = two; two = tmp; } |
<> MyArray<T>:: =( MyArray tmp ) { std::swap( numElements, tmp.numElements ); std::swap( pElements, tmp.pElements ); * ; } |
IMAGES
COMMENTS
The presence of the copy versions will prevent the implicit-declaration of the move constructor and move assignment operator, and declaring one form of a copy special member function suppresses the implicit-declaration of other forms. MyClass (const MyClass&) = delete; MyClass& operator= (const MyClass&) = delete;
Copy constructor (and assignment) should be defined when ever the implicitly generated one violates any class invariant. It should be defined as deleted when it cannot be written in a way that wouldn't have undesirable or surprising behaviour. Probably the simplest example is the class std::unique_ptr. As the name implies, it has unique ...
the copy assignment operator selected for every non-static class type (or array of class type) member of T is trivial. A trivial copy assignment operator makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially copy-assignable.
Benefits of explicitly defaulted and deleted functions. In C++, the compiler automatically generates the default constructor, copy constructor, copy-assignment operator, and destructor for a type if it doesn't declare its own. These functions are known as the special member functions, and they're what make simple user-defined types in C++ ...
Code language:C++(cpp) The = assignment operator is called a simple assignment operator. It assigns the value of the left operand to the right operand. Besides the simple assignment operator, C supports compound assignment operators. A compound assignment operator performs the operation specified by the additional operator and then assigns the ...
The copy assignment operator selected for every non-static class type (or array of class type) memeber of T is trivial. A trivial copy assignment operator makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially copy-assignable.
Use an assignment operator operator= that returns a reference to the class type and takes one parameter that's passed by const reference—for example ClassName& operator=(const ClassName& x);. Use the copy constructor. If you don't declare a copy constructor, the compiler generates a member-wise copy constructor for you.
for assignments to class type objects, the right operand could be an initializer list only when the assignment is defined by a user-defined assignment operator. removed user-defined assignment constraint. CWG 1538. C++11. E1 ={E2} was equivalent to E1 = T(E2) ( T is the type of E1 ), this introduced a C-style cast. it is equivalent to E1 = T{E2}
Overloads of operator delete and operator delete[] with additional user-defined parameters ("placement forms", ()) may be declared at global scope as usual, and are called by the matching placement forms of new-expressions if a constructor of the object that is being allocated throws an exception.. The standard library placement forms of operator delete cannot be replaced and can only be ...
Assignment Operators in C are used to assign values to the variables. They come under the category of binary operators as they require two operands to operate upon. The left side operand is called a variable and the right side operand is the value. The value on the right side of the "=" is assigned to the variable on the left side of "=".
Most concise way to disable copy and move semantics. According to this chart (by Howard Hinnant): The most concise way is to =delete move assignment operator (or move constructor, but it can cause problems mentioned in comments). Though, in my opinion the most readable way is to =delete both copy constructor and copy assignment operator.
C++. Copy. int* set = new int[100]; //use set[] delete [] set; Using the delete operator on an object deallocates its memory. A program that dereferences a pointer after the object is deleted can have unpredictable results or crash. When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the ...
Assignment performs implicit conversion from the value of rhs to the type of lhs and then replaces the value in the object designated by lhs with the converted value of rhs . Assignment also returns the same value as what was stored in lhs (so that expressions such as a = b = c are possible). The value category of the assignment operator is non ...
A class can have multiple copy assignment operators, e.g. both T & T:: operator = (const T &) and T & T:: operator = (T). If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default. (since C++11)
21.12 — Overloading the assignment operator. The copy assignment operator (operator=) is used to copy values from one object to another already existing object. As of C++11, C++ also supports "Move assignment". We discuss move assignment in lesson 22.3 -- Move constructors and move assignment .
A class can have multiple copy assignment operators, e.g. both T & T:: operator = (const T &) and T & T:: operator = (T). If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default. (since C++11)
Rule of three. If a class requires a user-defined destructor, a user-defined copy constructor, or a user-defined copy assignment operator, it almost certainly requires all three. Because C++ copies and copy-assigns objects of user-defined types in various situations (passing/returning by value, manipulating a container, etc), these special ...
Deleted implicitly-declared copy assignment operator. A implicitly-declared copy assignment operator for class T is defined as deleted if any of the following is true: • T has a user-declared move constructor; • T has a user-declared move assignment operator. Otherwise, it is defined as defaulted.
The move assignment operator is called whenever it is selected by overload resolution, e.g. when an object appears on the left-hand side of an assignment expression, where the right-hand side is an rvalue of the same or implicitly convertible type.. Move assignment operators typically "steal" the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors ...
delete on the same non-NULL pointer, heap corruption results. Rarely you will come across a class that does not contain raw ... assignment operator, the compiler gives you one implicitly. The implicit assignment operator does member-wise assignment of each data member from the source object. For example, using