File: revising2.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (33 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)