File: moveassignment.yo

package info (click to toggle)
c%2B%2B-annotations 10.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,536 kB
  • ctags: 3,247
  • sloc: cpp: 19,157; makefile: 1,521; ansic: 165; sh: 128; perl: 90
file content (29 lines) | stat: -rw-r--r-- 1,222 bytes parent folder | download | duplicates (3)
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
In addition to the overloaded assignment operator a emi(move assignment)
operator may be implemented for classes supporting move operations. In this
case, if the class supports swapping the implementation is surprisingly
simple. No copy construction is required and the move assignment operator can
simply be implemented like this:
        verb(
    Class &operator=(Class &&tmp)
    {
        swap(tmp);
        return *this;
    }
        )
    If swapping is not supported then the assignment can be performed for each
of the data members in turn, using tt(std::move) as shown in the previous
section with a class tt(Person). Here is an example showing how to do this
with that class  tt(Person):
        verb(
    Person &operator=(Person &&tmp)
    {
        d_name = std::move(tmp.d_name);
        d_address = std::move(tmp.d_address);
        return *this;
    }
        )
    As noted previously (section ref(MOVECONS)) declaring a move assignment
operator suppresses the default availability of the copy constructor. It is
made available again by declaring the copy constructor in the class's
interface (and of course by providing an explicit implementation or by using
the tt(= default) default implementation).