Files
2025-10-25 03:02:53 +03:00

57 lines
2.2 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[class.nest]
# 11 Classes [[class]](./#class)
## 11.4 Class members [[class.mem]](class.mem#class.nest)
### 11.4.12 Nested class declarations [class.nest]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3111)
A class can be declared within another class[.](#1.sentence-1)
A class declared within
another is called a [*nested class*](#def:class,nested "11.4.12Nested class declarations[class.nest]")[.](#1.sentence-2)
[*Note [1](#note-1)*:
See [[expr.prim.id]](expr.prim.id "7.5.5Names") for restrictions on the use of non-static data
members and non-static member functions[.](#1.sentence-3)
— *end note*]
[*Example [1](#example-1)*: int x;int y;
struct enclose {int x; static int s; struct inner {void f(int i) {int a = sizeof(x); // OK, operand of sizeof is an unevaluated operand x = i; // error: assign to enclose::x s = i; // OK, assign to enclose::s::x = i; // OK, assign to global x y = i; // OK, assign to global y}void g(enclose* p, int i) { p->x = i; // OK, assign to enclose::x}};};
inner* p = 0; // error: inner not found — *end example*]
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3146)
[*Note [2](#note-2)*:
Nested classes can be defined
either in the enclosing class or in an enclosing namespace;
member functions and static data members of a nested class can be
defined either in the nested class or in an enclosing namespace scope[.](#2.sentence-1)
[*Example [2](#example-2)*: struct enclose {struct inner {static int x; void f(int i); };};
int enclose::inner::x = 1;
void enclose::inner::f(int i) { /* ... */ }class E {class I1; // forward declaration of nested classclass I2; class I1 { }; // definition of nested class};class E::I2 { }; // definition of nested class — *end example*]
— *end note*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L3175)
A friend function ([[class.friend]](class.friend "11.8.4Friends")) defined
within a nested class has no special access rights to
members of an enclosing class[.](#3.sentence-1)