Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 872 Bytes

overload_assignment.md

File metadata and controls

31 lines (25 loc) · 872 Bytes

Overloading the Assignment Operator

The assignment operator should assign one object to the value of another. The assigned object should be returned by reference. It must be defined as a member function.

Example

struct point {
    int x, y;
    // copy assignment, user-defined
    point& operator=(const point& other) {
        this->x = other.x;
        this->y = other.y;
        return *this;
    }
    // move assignment, explicitly defaulted
    point& operator=(point&&) = default;
};

Note: the copy assignment operator can accept other by value too.

See Also

<:cppreference:875716540929015908> Copy Assignment Operator
<:cppreference:875716540929015908> Move Assignment Operator

?creditFooter 162964325823283200