File: nesting.yo

package info (click to toggle)
c%2B%2B-annotations 13.04.01-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,636 kB
  • sloc: cpp: 25,471; makefile: 1,521; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (151 lines) | stat: -rw-r--r-- 6,097 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Classes can be defined inside other classes. Classes defined inside
other classes are called hi(nested class) em(nested classes). Nested classes
are used in situations where the nested class has a close conceptual
relationship to its surrounding class. For example, with the class tt(string)
a type ti(string::iterator) is available which provides all characters
that are stored in the tt(string). This tt(string::iterator) type could be
defined as an object ti(iterator), defined as nested class in the class
tt(string). 

Since  nested classes are defined inside other classes their members, when
provided with references or pointers to objects of their surrounding classes,
may access all members of those objects, even their private members.

A class can be nested in every part of the surrounding class: in the
tt(public, protected) or tt(private) section. If a class is nested in the
tt(public) section of a class, it is
    hi(visibility: nested classes) visible outside the surrounding class. If
it is nested in the tt(protected) section it is visible in subclasses, derived
from the surrounding class, if it is nested in the tt(private) section, it is
only visible for the members of the surrounding class.

The surrounding class has no special privileges with respect to the nested
class. For example, consider the following class definition:
        verb(    class Surround
    {
        public:
            class FirstWithin
            {
                int d_variable;

                public:
                    FirstWithin();
                    int var() const;
            };
        private:
            class SecondWithin
            {
                int d_variable;

                public:
                    SecondWithin();
                    int var() const;
            };
    };
    inline int Surround::FirstWithin::var() const
    {
        return d_variable;
    }
    inline int Surround::SecondWithin::var() const
    {
        return d_variable;
    })

In the annotations(), in order to save space, nested class interfaces are
usually declared inside their surrounding class, as shown above. Often this
can be avoided, which is desirable as it more clearly separates the outer
class's interface and the nested class's interface. Likewise, in-class member
implementations should be avoided. Here is an illustration of how outer- and
nested class interfaces can be separated:
        verb(    class Surround
    {
        class SecondWithin;

        public:
            class FirstWithin;
    };

    class Surround::FirstWithin
    {
        int d_variable;

        public:
            FirstWithin();
            int var() const;
    };

    class Surround::SecondWithin
    {
        int d_variable;

        public:
            SecondWithin();
            int var() const;
    };)

For these three classes access to members is defined as follows:
        itemization(
        it() The class tt(Surround::FirstWithin) is visible outside and inside
tt(Surround). The class tt(Surround::FirstWithin) thus has  global visibility.
        it() tt(FirstWithin)'s constructor and its member function
tt(var) are also globally visible.
        it() The data member tt(d_variable) is only visible to the members of
the class tt(Surround::FirstWithin). Neither the members of tt(Surround) nor
the members of tt(Surround::SecondWithin) can directly access
tt(Surround::FirstWithin::d_variable).
        it() The class tt(Surround::SecondWithin) is only visible inside
tt(Surround). The public members of the class tt(Surround::SecondWithin) can
also be used by the members of the class tt(Surround::FirstWithin), as nested
classes can be considered members of their surrounding class.
        it() tt(Surround::SecondWithin)'s constructor and its member function
tt(var) also can only be reached by the members of tt(Surround) (and by the
members of its nested classes).
        it() tt(Surround::SecondWithin::d_variable) is only visible to
tt(Surround::SecondWithin)'s members. Neither the members of tt(Surround) nor
the members of tt(Surround::FirstWithin) can access tt(d_variable) of the
class tt(Surround::SecondWithin) directly.
        it() As always, an object of the class type is required before
its members can be called. This also holds true for nested classes.
        )
    To grant the surrounding class access rights to the private members of a
nested class the nested class may declare its surrounding class as a
friend. Conversely, as nested classes can be considered members of their
surrounding class their member functions have full access to the outer class
members, if they are provided with an outer class object (see section
ref(NESTEDFRIENDS)).

    Although nested classes can be considered members of the surrounding
class, members of nested classes are em(not) members of the surrounding
class: members of the class tt(Surround) may not directly call
tt(FirstWithin::var). This is understandable considering that a
tt(Surround) object is not also a tt(FirstWithin) or tt(SecondWithin)
object. In fact, nested classes are just typenames. It is not implied that
objects of such classes automatically exist in the surrounding class. If a
member of the surrounding class should use a (non-static) member of a nested
class then the surrounding class must define a nested class object, which can
thereupon be used by the members of the surrounding class to use members of
the nested class.

    For example, in the following class definition there is a surrounding
class tt(Outer) and a nested class tt(Inner). The class tt(Outer) contains a
member function tt(caller). The member function tt(caller) uses the
tt(d_inner) object that is composed within tt(Outer) to call
tt(Inner::infunction):
        verb(    class Outer
    {
        public:
            void caller();

        private:
            class Inner
            {
                public:
                    void infunction();
            };
            Inner d_inner;      // class Inner must be known
    };
    void Outer::caller()
    {
        d_inner.infunction();
    })