File: explicit.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (75 lines) | stat: -rw-r--r-- 3,467 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
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
72
73
74
75
Consider the following situations:
    itemization(
    it() A class tt(Value) is a em(value) class. It offers a copy constructor,
an overloaded assignment operator, maybe move operations, and a public,
non-virtual constructor. In section ref(INHERITWHY) it is argued that such
classes are not suited as base classes. New classes should not inherit from
tt(Value). How to enforce this?
    it() A polymorphic class tt(Base) defines a virtual member
tt(v_process(int32_t)). A class derived from tt(Base) needs to override this
member, but the author mistakingly defined tt(v_proces(int32_t)). How to
prevent such errors, breaking the polymorphic behavior of the derived class?
    it() A class tt(Derived), derived from a polymorphic tt(Base) class
overrides the member tt(Base::v_process), but classes that are in turn derived
from tt(Derived) should no longer override tt(v_process), but em(may) override
other virtual members like tt(v_call) and tt(v_display). How to enforce this
restricted polymorphic character for classes derived from tt(Derived)?
    )
    Two special identifiers, ti(final) and ti(override) are used to realize
the above. These identifiers are special in the sense that they only require
their special meanings in specific contexts. Outside of this context they are
just plain identifiers, allowing the programmer to define a variable like
tt(bool final).

    The identifier tt(final) can be applied to class declarations to indicate
that the class cannot be used as a base class. E.g.:
        verb(    class Base1 final               // cannot be a base class
    {};
    class Derived1: public Base1    // ERR: Base1 is final
    {};

    class Base2                     // OK as base class
    {};
    class Derived2 final: public Base2  // OK, but Derived2 can't be
    {};                                 //     used as a base class
    class Derived: public Derived2      // ERR: Derived2 is final
    {};)

The identifier tt(final) can also be added to virtual member
declarations. This indicates that those virtual members cannot be overridden
by derived classes. The restricted polymorphic character of a class, mentioned
above, can thus be realized as follows:
        verb(    class Base
    {
        virtual int v_process();    // define polymorphic behavior
        virtual int v_call();
        virtual int v_display();
    };
    class Derived: public Base      // Derived restricts polymorphism
    {                               // to v_call and v_display
        virtual int v_process() final;
    };
    class Derived2: public Derived
    {
        // int v_process();            No go: Derived:v_process is final
        virtual int v_display();    // OK to override
    };)

To allow the compiler to detect typos, differences in parameter types, or
differences in member function modifiers (e.g., tt(const) vs. non-tt(const))
the identifier tt(override) can (should) be appended to derived class members
overriding base class members. E.g.,
        verb(    class Base
    {
        virtual int v_process();
        virtual int v_call() const;
        virtual int v_display(std::ostream &out);
    };
    class Derived: public Base
    {
        virtual int v_proces() override;    // ERR: v_proces != v_process
        virtual int v_call() override;      // ERR: not const
                                            // ERR: parameter types differ
        virtual int v_display(std::istream &out) override;
    };)