Assignment operator (C++)
Operator (=) used for assigning values in C++ / From Wikipedia, the free encyclopedia
Dear Wikiwand AI, let's keep it short by simply answering these key questions:
Can you list the top facts and stats about Assignment operator in C++?
Summarize this article for a 10 year old
In the C++ programming language, the assignment operator, =
, is the operator used for assignment. Like most other operators in C++, it can be overloaded.
The copy assignment operator, often just called the "assignment operator", is a special case of assignment operator where the source (right-hand side) and destination (left-hand side) are of the same class type. It is one of the special member functions, which means that a default version of it is generated automatically by the compiler if the programmer does not declare one. The default version performs a memberwise copy, where each member is copied by its own copy assignment operator (which may also be programmer-declared or compiler-generated).
The copy assignment operator differs from the copy constructor in that it must clean up the data members of the assignment's target (and correctly handle self-assignment) whereas the copy constructor assigns values to uninitialized data members.[1] For example:
My_Array first; // initialization by default constructor
My_Array second(first); // initialization by copy constructor
My_Array third = first; // Also initialization by copy constructor
second = third; // assignment by copy assignment operator