Jump to content

C++: Difference between revisions

Line 663: Line 663:
* Copy assignment operator
* Copy assignment operator


Rule of five:
[[Wikipedia: Rule of three (C++ programming)#Rule of five | Rule of five]]
* Swap function (for <code>std::swap</code>)
* Move constructor
* Move constructor (since C++11, for <code>std::move</code>)
* Move assignment operator
 
{{hidden | Example RAII Class |
{{hidden | Example RAII Class |
Copied from [https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom stack overflow]
Copied from [https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom stack overflow]
Line 699: Line 700:
     }
     }


    // copy assignment
    dumb_array& operator=(dumb_array other) // (1)
    {
        swap(*this, other); // (2)
        return *this;
    }
    // swap
     friend void swap(dumb_array& first, dumb_array& second) // nothrow
     friend void swap(dumb_array& first, dumb_array& second) // nothrow
     {
     {
Line 710: Line 720:
     }
     }


     dumb_array& operator=(dumb_array other) // (1)
     // move constructor
    {
        swap(*this, other); // (2)
 
        return *this;
    }
 
     dumb_array(dumb_array&& other) noexcept ††
     dumb_array(dumb_array&& other) noexcept ††
         : dumb_array() // initialize via default constructor, C++11 only
         : dumb_array() // initialize via default constructor, C++11 only