[PATCH] cleaning before = assignment lotuswordpro/source/filter/xfilter/xfparastyle.cxx

David Tardon dtardon at redhat.com
Mon Jul 16 00:23:44 PDT 2012


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.


More information about the LibreOffice mailing list