File: operator.yo

package info (click to toggle)
c%2B%2B-annotations 8.2.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,804 kB
  • ctags: 2,845
  • sloc: cpp: 15,418; makefile: 2,473; ansic: 165; perl: 90; sh: 29
file content (71 lines) | stat: -rw-r--r-- 2,821 bytes parent folder | download
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
To add operator overloading to  a class, the class interface is simply
provided  with a (usually em(public)) member function naming the particular
operator. That member function is thereupon implemented.

To overload the assignment operator tt(=), a member tt(operator=(Class const
&rhs)) is added to the class interface.  Note that the function name consists
of two parts: the keyword ti(operator), followed by the operator itself.  When
we augment a class interface with a member function tt(operator=), then that
operator is em(redefined) for the class, which prevents the default operator
from being used. In the link(previous section)(OVERLOADASSIGN) the function
tt(assign) was provided to solve the problems resulting from using the
default assignment operator. Rather than using an ordinary member
function bf(C++) commonly uses a dedicated operator generalizing the
operator's default behavior to the class in which it is defined.

The tt(assign) member mentioned before may be redefined as follows (the member
tt(operator=) presented below is a first, rather unsophisticated, version of
the overloaded assignment operator.  It will shortly be improved):
        verb(
    class Person
    {
        public:                             // extension of the class Person
                                            // earlier members are assumed.
            void operator=(Person const &other);
    };
        )
    Its implementation could be
        verb(
    void Person::operator=(Person const &other)
    {
        delete[] d_name;                      // delete old data
        delete[] d_address;
        delete[] d_phone;

        d_name = strdupnew(other.d_name);   // duplicate other's data
        d_address = strdupnew(other.d_address);
        d_phone = strdupnew(other.d_phone);
    }
        )
    This member's actions are similar to those of the previously mentioned
member tt(assign), but this member is automatically called when the assignment
operator tt(=) is used. Actually there are em(two) ways to
call overloaded operators as shown in the next example:
        verb(
    void tmpPerson(Person const &person)
    {
        Person tmp;

        tmp = person;
        tmp.operator=(person);  // the same thing
    }
        )
    Overloaded operators are seldom called explicitly, but explicit calls must
be used (rather than using the plain operator syntax) when you explicitly
em(want) to call the overloaded operator from a pointer to an object (it is
also possible to dereference the pointer first and then use the plain operator
syntax, see the next example):
        verb(
    void tmpPerson(Person const &person)
    {
        Person *tmp = new Person;

        tmp->operator=(person);
        *tmp = person;          // yes, also possible...

        delete tmp;
    }
        )