encyclopegia.org

Assignment operator (C++)

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. For example: MyArray first; // initialization by default constructor MyArray second(first); // initialization by copy constructor MyArray third = first; // Also initialization by copy constructor second = third; // assignment by copy assignment operator

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. For example: MyArray first; // initialization by default constructor MyArray second(first); // initialization by copy constructor MyArray third = first; // Also initialization by copy constructor second = third; // assignment by copy assignment operator ==Return value of overloaded assignment operator== The language permits an overloaded assignment operator to have an arbitrary return type (including void ). However, the operator is usually defined to return a reference to the assignee. This is consistent with the behavior of assignment operator for built-in types (returning the assigned value) and allows for using the operator invocation as an expression, for instance in control statements or in chained assignment. Also, the C++ Standard Library requires this behavior for some user-supplied types. ==Overloading copy assignment operator== When deep copies of objects have to be made, exception safety should be taken into consideration. One way to achieve this when resource deallocation never fails is: Acquire new resources Release old resources Assign the new resources' handles to the object class MyArray{ int* array; int count; public: MyArray& operator=(const MyArray& other) { if (this != &other) { // protect against invalid self-assignment // 1: allocate new memory and copy the elements int* new_array = new int[other.count]; std::copy(other.array, other.array + other.count, new_array); // 2: deallocate old memory delete[] array; // 3: assign the new memory to the object array = new_array; count = other.count; } // by convention, always return *this return *this; } // ... }; However, if a no-fail (no-throw) swap function is available for all the member subobjects and the class provides a copy constructor and destructor (which it should do according to the rule of three), the most straightforward way to implement copy assignment is as follows: public: // the swap member function (should never fail!) void swap(MyArray& other) { // swap all the members (and base subobject, if applicable) with other using std::swap; // because of ADL the compiler will use // custom swap for members if it exists // falling back to std::swap swap(array, other.array); swap(count, other.count); } // note: argument passed by value! MyArray& operator=(MyArray other) { // swap this with other swap(other); // by convention, always return *this return *this; // other is destroyed, releasing the memory } ==Assignment between different classes== C++ supports assignment between different classes, both via implicit copy constructor and assignment operator, if the destination instance class is the ancestor of the source instance class: class Ancestor { public: int a; }; class Descendant : public Ancestor { public: int b; }; int main() { Descendant d; Ancestor a(d); Ancestor b(d); a = d; } Copying from ancestor to descendant objects, which could leave descendant's fields uninitialized, is not permitted. ==See also== Operator overloading Move assignment operator Rule of three (C++ programming) Operators in C and C++ ==References== ==External links== The Anatomy of the Assignment Operator, by Richard Gillam

References

No reference rows published for this article.

Cite this article

MLA

"Assignment operator (C++)." encyclopegia.org, 2025, https://encyclopegia.org/wiki/assignment-operator-c. Accessed 24 November 2026.

Chicago

"Assignment operator (C++)." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/assignment-operator-c.

APA

encyclopegia.org. (2025). Assignment operator (C++). Retrieved November 24, 2026, from https://encyclopegia.org/wiki/assignment-operator-c

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1309004124 · 0.5 · snapshot enwiki-001309004124

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.