Date: prev next · Thread: first prev next last
2012 Archives by date, by thread · List index


On Sat, Jul 14, 2012 at 07:04:07AM -0700, julien2412 wrote:
Hello,

Keeping on slowly reading Bjarne Stroustrup's C++ book, I read that for =
operator functions, we must delete/free the members before = assignment
(which must copy).

Yes. Additionally, we must guard against self-assignment, because it
would lead to use of already deleted object. So, the canonical way to
write operator= is:

Foo& operator=(Foo const& other)
{
    if (this != &other)
    {
        // copy
    }
    return *this;
}

Or, if the class implements swap (and has accessible copy constructor,
which it should always have when it has operator=):

Foo& operator=(Foo const& other)
{
    Foo copy(other);
    swap(copy);
    return *this;
}

D.

Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.