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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
To grant nested classes access rights to the private members of other nested
classes, or to grant a surrounding class access to the private members of its
nested classes the hi(friend: nested classes)tt(friend) keyword must be used.
No friend declaration is required to grant a nested class access to the
private members of its surrounding class. Static members of the surrounding
class can directly be accessed, other members can be accessed if a surrounding
class object is defined by or passed to members of the nested class. After
all, a nested class is a type defined by its surrounding class and as such
objects of the nested class are members of the outer class and thus can access
all the outer class's members. Here is an example showing this principle. The
example won't compile as members of the class tt(Extern) are denied access to
tt(Outer)'s private members, but tt(Outer::Inner)'s members em(can) access
tt(Outer)'s private members:
verb( class Outer
{
int d_value;
static int s_value;
public:
Outer()
:
d_value(12)
{}
class Inner
{
public:
Inner()
{
cout << "Outer's static value: " << s_value << '\n';
}
Inner(Outer &outer)
{
cout << "Outer's value: " << outer.d_value << '\n';
}
};
};
class Extern // won't compile!
{
public:
Extern(Outer &outer)
{
cout << "Outer's value: " << outer.d_value << '\n';
}
Extern()
{
cout << "Outer's static value: " << Outer::s_value << '\n';
}
};
int Outer::s_value = 123;
int main()
{
Outer outer;
Outer::Inner in1;
Outer::Inner in2{ outer };
})
Now consider the situation where a class tt(Surround) has two nested
classes tt(FirstWithin) and tt(SecondWithin). Each of the three classes has a
static data member tt(int s_variable):
verb( class Surround
{
static int s_variable;
public:
class FirstWithin
{
static int s_variable;
public:
int value();
};
int value();
private:
class SecondWithin
{
static int s_variable;
public:
int value();
};
};)
If the class tt(Surround) should be able to access tt(FirstWithin) and
tt(SecondWithin)'s private members, these latter two classes must declare
tt(Surround) to be their friend. The function tt(Surround::value) can
thereupon access the private members of its nested classes. For example (note
the tt(friend) declarations in the two nested classes):
verb( class Surround
{
static int s_variable;
public:
class FirstWithin
{
friend class Surround;
static int s_variable;
public:
int value();
};
int value();
private:
class SecondWithin
{
friend class Surround;
static int s_variable;
public:
int value();
};
};
inline int Surround::FirstWithin::value()
{
FirstWithin::s_variable = SecondWithin::s_variable;
return (s_variable);
})
Friend declarations may be provided em(beyond) the definition of the
entity that is to be considered a friend. So a class can be declared a friend
em(beyond) its definition. In that situation in-class code may already use the
fact that it is going to be declared a friend by the upcoming class. As an
example, consider an in-class implementation of the function
tt(Surround::FirstWithin::value). The required tt(friend) declaration can also
be inserted em(after) the implementation of the function tt(value):
verb( class Surround
{
public:
class FirstWithin
{
static int s_variable;
public:
int value();
{
FirstWithin::s_variable = SecondWithin::s_variable;
return s_variable;
}
friend class Surround;
};
private:
class SecondWithin
{
friend class Surround;
static int s_variable;
};
};)
Note that members named identically in outer and inner classes
(e.g., `tt(s_variable)')
hi(nested class: member access)
may be accessed using the proper scope resolution expressions, as
illustrated below:
verb( class Surround
{
static int s_variable;
public:
class FirstWithin
{
friend class Surround;
static int s_variable; // identically named
public:
int value();
};
int value();
private:
class SecondWithin
{
friend class Surround;
static int s_variable; // identically named
public:
int value();
};
static void classMember();
};
inline int Surround::value()
{ // scope resolution expression
FirstWithin::s_variable = SecondWithin::s_variable;
return s_variable;
}
inline int Surround::FirstWithin::value()
{
Surround::s_variable = 4; // scope resolution expressions
Surround::classMember();
return s_variable;
}
inline int Surround::SecondWithin::value()
{
Surround::s_variable = 40; // scope resolution expression
return s_variable;
})
Nested classes aren't automatically each other's friends. Here tt(friend)
declarations must be provided to grant one nested classes access to another
nested class's private members.
To grant tt(FirstWithin) access to tt(SecondWithin)'s private members,
tt(SecondWithin) must contain a tt(friend) declaration.
Likewise, the class tt(FirstWithin) simply uses tt(friend class
SecondWithin) to grant tt(SecondWithin) access to tt(FirstWithin)'s private
members. Even though the compiler hasn't seen tt(SecondWithin) yet, a friend
declaration is also considered a forward declaration.
Note that tt(SecondWithin)'s forward declaration cannot be specified inside
tt(FirstWithin) by using `tt(class Surround::SecondWithin;)', as this would
generate an error message like:
quote(`Surround' does not have a nested type named `SecondWithin')
Now assume that in addition to the nested class tt(SecondWithin) there also
exists an outer-level class tt(SecondWithin). To declare that class a friend
of tt(FirstWithin's) declare tt(friend ::SecondWithin) inside tt(class
FirstWithin). In that case, an outer level class declaration of
tt(FirstWithin) must be provided before the compiler encounters the tt(friend
::SecondWithin) declaration.
Here is an example in which all classes have full access to all private
members of all involved classes: hi(friend: as forward declaration), and a
outer level tt(FirstWithin) has also been declared:
verb( class SecondWithin;
class Surround
{
// class SecondWithin; not required (but no error either):
// friend declarations (see below)
// are also forward declarations
static int s_variable;
public:
class FirstWithin
{
friend class Surround;
friend class SecondWithin;
friend class ::SecondWithin;
static int s_variable;
public:
int value();
};
int value(); // implementation given above
private:
class SecondWithin
{
friend class Surround;
friend class FirstWithin;
static int s_variable;
public:
int value();
};
};
inline int Surround::FirstWithin::value()
{
Surround::s_variable = SecondWithin::s_variable;
return s_variable;
}
inline int Surround::SecondWithin::value()
{
Surround::s_variable = FirstWithin::s_variable;
return s_variable;
})
|