cppannotations/annotations/yo/memory/revising2.yo
2017-06-06 08:09:38 +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)