File: insertion.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 (38 lines) | stat: -rw-r--r-- 2,052 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
34
35
36
37
38
Classes also frequently define overloaded insertion and extraction
operators. Since there are no `compound insertion operators' the design shown
so far cannot be used when overloading these operators. Instead using
standardized member function signatures is advocated: tt(void
insert(std::ostream &out) const) to insert an object into an tt(ostream) and
tt(void extract(std::istream &in) const) to extract an object from an
tt(istream). As these functions are only used by, respectively, the insertion
and extraction operators, they can be declared in the tt(Derived) class's
private interface. Instead of declaring the insertion and extraction operators
friends of the class tt(Derived) a single tt(friend Binops<Derived>) is
specified. This allows tt(Binops<Derived>) to define private, inline tt(iWrap)
and tt(eWrap) members, merely calling, respectively, tt(Derived's insert) and
tt(extract) members:
        verb(    template <typename Derived>
    inline void Binops<Derived>::iWrap(std::ostream &out) const
    {
        static_cast<Derived const &>(*this).insert(out);
    })

tt(Binops<Derived>) then declares the insertion and extraction operators as
its friends, allowing these operators to call, respectively, tt(iWrap) and
tt(eWrap). Note that the software engineer designing the class tt(Derived)
only has to provide a tt(friend Binops<Derived>) declaration. Here is the
implementation of the overloaded insertion operator:
        verb(    template <typename Derived>
    std::ostream &operator<<(std::ostream &out, Binops<Derived> const &obj)
    {
        obj.iWrap(out);
        return out;
    })

This completes the coverage of the essentials of a class template tt(Binops)
potentially offering binary operators and insertion/extraction operators for
any class derived from tt(Binops). Finally, as noted at the beginning of this
section, a complete implementation of a class offering addition and insertion
operators is provided in the file
tt(annotations/yo/concrete/examples/binopclasses.cc) in the annotations()'
source archive.