cppannotations/annotations/yo/memory/revising2.yo
Frank B. Brokken 777b182edd Moved all files but 'excluded', 'sf', and 'sourcetar' to ./annotations
This allowed me to standardize the sourcetar and sf/* scripts: the base
    directory (containing ./git) is now empty, except for maintenance scripts,
    while the source files and build scripts of the annotations are stored in
    a subdirectory of their own.
2013-05-29 20:44:08 +02:00

36 lines
1.2 KiB
Text

Now that we've familiarized ourselves with the overloaded assignment operator
and the move-assignment, let's once again have a look at their
implementations for a class tt(Class), supporting swapping through its
tt(swap) member. Here is the generic implementation of the overloaded
assignment operator:
verb(
Class &operator=(Class const &other)
{
Class tmp(other);
swap(tmp);
return *this;
}
)
and this is the move-assignment operator:
verb(
Class &operator=(Class &&tmp)
{
swap(tmp);
return *this;
}
)
They look remarkably similar in the sense that the overloaded assignment
operator's code is identical to the move-assignment operator's code once a
copy of the tt(other) object is available. Since the overloaded assignment
operator's tt(tmp) object really is nothing but a temporary tt(Class) object
we can use this fact by implementing the overloaded assignment operator in
terms of the move-assignment. Here is a second revision of the overloaded
assignment operator:
verb(
Class &operator=(Class const &other)
{
Class tmp(other);
return *this = std::move(tmp);
}
)
COMMENT(Demo in examples/moveassign.cc)