File: baseclass.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,728 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
Although the em(construction) of class templates is the topic of chapter
ref(TEMPCLASS) class templates have already extensively been em(used)
earlier. For example, abstract containers (cf. chapter ref(CONTAINERS)) are
defined as class templates.  Class templates can, like
 hi(ordinary class)hi(class: ordinary)
    ordinary classes, participate in the construction of class hierarchies.

    In section ref(DERIVEDTEMPCLASS) it is shown how a i(class template) can
be derived from another class template.

    As class template derivation remains to be covered, the following
discussion is necessarily somewhat premature. The reader may of course skip
briefly to section ref(DERIVEDTEMPCLASS) returning back to this section
thereafter.

    In this section it should be assumed, for the sake of argument, that a
class template tt(Vector) has somehow been derived from a tt(std::vector).
Furthermore, assume that the following function template has been
constructed to sort a vector using some function object tt(obj):
        verb(
    template <typename Type, typename Object>
    void sortVector(std::vector<Type> vect, Object const &obj)
    {
        sort(vect.begin(), vect.end(), obj);
    }
        )
    To sort tt(std::vector<string>) objects case-insensitively, a tt(class
Caseless) could be constructed as follows:
        verb(
    class CaseLess
    {
        public:
            bool operator()(std::string const &before,
                            std::string const &after) const
            {
                return strcasecmp(before.c_str(), after.c_str()) < 0;
            }
    };
        )
    Now various vectors may be sorted using tt(sortVector()):
        verb(
    int main()
    {
        std::vector<string> vs;
        std::vector<int> vi;

        sortVector(vs, CaseLess());
        sortVector(vi, less<int>());
    }
        )
    Applying the transformation
        hi(transformation to a base class)
        hi(class template: transformation to a base class)
    em(transformation to a base class instantiated from a class template), the
function template tt(sortVectors()) may now also be used to sort tt(Vector)
objects. For example:
        verb(
    int main()
    {
        Vector<string> vs;      // `Vector' instead of `std::vector'
        Vector<int> vi;

        sortVector(vs, CaseLess());
        sortVector(vi, less<int>());
    }
        )
    In this example, tt(Vector)s were passed as argument to
tt(sortVector). Applying the transformation to a base class instantiated from
a class template, the compiler considers tt(Vector) to be a tt(std::vector)
enabling it to deduce the template's type parameter. A tt(std::string) for the
tt(Vector vs), an tt(int) for tt(Vector vi).