______________________________________________________________________
9 Classes [class]
______________________________________________________________________
1 A class is a type. Its name becomes a class-name (_class.name_)
within its scope.
class-name:
identifier
template-id
Class-specifiers and elaborated-type-specifiers (_dcl.type.elab_) are
used to make class-names. An object of a class consists of a (possi
bly empty) sequence of members and base class objects.
class-specifier:
class-head { member-specificationopt }
class-head:
class-key identifieropt base-clauseopt
class-key nested-name-specifier identifier base-clauseopt
class-key:
class
struct
union
2 The name of a class can be used as a class-name even within the base-
clause and member-specification of the class specifier itself. A
class-specifier is commonly referred to as a class definition. A
class is considered defined after the closing brace of its class-
specifier has been seen even though its member functions are in gen
eral not yet defined.
3 Objects of an empty class have a nonzero size.
+------- BEGIN BOX 1 -------+
Bill Gibbons suggest that a base class subobject should be allowed to
occupy zero bytes of the complete object. This would permit two base
class subobjects to have the same address, for example.
+------- END BOX 1 -------+
4 Class objects can be assigned, passed as arguments to functions, and
returned by functions (except objects of classes for which copying has
been restricted; see _class.copy_). Other plausible operators, such
as equality comparison, can be defined by the user; see _over.oper_.
5 A structure is a class declared with the class-key struct; its members
and base classes (_class.derived_) are public by default
(_class.access_). A union is a class declared with the class-key
union; its members are public by default and it holds only one member
at a time (_class.union_).
6 Aggregates of class type are described in _dcl.init.aggr_. A
POD-struct1) is an aggregate class that has no members of type refer
ence, pointer to member, non-POD-struct or non-POD-union. Similarly,
a POD-union is an aggregate union that has no members of type refer
ence, pointer to member, non-POD-struct or non-POD-union.
9.1 Class names [class.name]
1 A class definition introduces a new type. For example,
struct X { int a; };
struct Y { int a; };
X a1;
Y a2;
int a3;
declares three variables of three different types. This implies that
a1 = a2; // error: Y assigned to X
a1 = a3; // error: int assigned to X
are type mismatches, and that
int f(X);
int f(Y);
declare an overloaded (_over_) function f() and not simply a single
function f() twice. For the same reason,
struct S { int a; };
struct S { int a; }; // error, double definition
is ill-formed because it defines S twice.
2 A class definition introduces the class name into the scope where it
is defined and hides any class, object, function, or other declaration
of that name in an enclosing scope (_basic.scope_). If a class name
is declared in a scope where an object, function, or enumerator of the
same name is also declared, then when both declarations are in scope,
the class can be referred to only using an elaborated-type-specifier
(_dcl.type.elab_). For example,
struct stat {
// ...
};
stat gstat; // use plain `stat' to
// define variable
int stat(struct stat*); // redefine `stat' as function
void f()
{
struct stat* ps; // `struct' prefix needed
// to name struct stat
// ...
stat(ps); // call stat()
// ...
}
A declaration consisting solely of class-key identifier ; is either a
_________________________
1) The acronym POD stands for plain ol' data.
redeclaration of the name in the current scope or a forward declara
tion of the identifier as a class name. It introduces the class name
into the current scope. For example,
struct s { int a; };
void g()
{
struct s; // hide global struct `s'
s* p; // refer to local struct `s'
struct s { char* p; }; // declare local struct `s'
struct s; // receclaration, has no effect
}
Such declarations allow definition of classes that refer to each
other. For example,
class vector;
class matrix {
// ...
friend vector operator*(matrix&, vector&);
};
class vector {
// ...
friend vector operator*(matrix&, vector&);
};
Declaration of friends is described in _class.friend_, operator func
tions in _over.oper_.
3 An elaborated-type-specifier (_dcl.type.elab_) can also be used in the
declarations of objects and functions. It differs from a class decla
ration in that if a class of the elaborated name is in scope the elab
orated name will refer to it. For example,
struct s { int a; };
void g(int s)
{
struct s* p = new struct s; // global `s'
p->a = s; // local `s'
}
4 A name declaration takes effect immediately after the identifier is
seen. For example,
class A * A;
first specifies A to be the name of a class and then redefines it as
the name of a pointer to an object of that class. This means that the
elaborated form class A must be used to refer to the class. Such
artistry with names can be confusing and is best avoided.
5 A typedef-name (_dcl.typedef_) that names a class is a class-name, but
shall not be used in an elaborated-type-specifier; see also
_dcl.typedef_.
9.2 Class members [class.mem]
member-specification:
member-declaration member-specificationopt
access-specifier : member-specificationopt
member-declaration:
decl-specifier-seqopt member-declarator-listopt ;
function-definition ;opt
qualified-id ;
using-declaration
member-declarator-list:
member-declarator
member-declarator-list , member-declarator
member-declarator:
declarator pure-specifieropt
declarator constant-initializeropt
identifieropt : constant-expression
pure-specifier:
= 0
constant-initializer:
= constant-expression
1 The member-specification in a class definition declares the full set
of members of the class; no member can be added elsewhere. Members of
a class are data members, member functions (_class.mfct_), nested
types, and member constants. Data members and member functions are
static or nonstatic; see _class.static_. Nested types are classes
(_class.name_, _class.nest_) and enumerations (_dcl.enum_) defined in
the class, and arbitrary types declared as members by use of a typedef
declaration (_dcl.typedef_). The enumerators of an enumeration
(_dcl.enum_) defined in the class are member constants of the class.
Except when used to declare friends (_class.friend_) or to adjust the
access to a member of a base class (_class.access.dcl_), member-
declarations declare members of the class, and each such member-
declaration must declare at least one member name of the class. A
member shall not be declared twice in the member-specification, except
that a nested class can be declared and then later defined.
2 Note that a single name can denote several function members provided
their types are sufficiently different (_over_).
3 A member-declarator can contain a constant-initializer only if it
declares a static member (_class.static_) of integral or enumeration
type, see _class.static.data_.
4 A member can be initialized using a constructor; see _class.ctor_.
5 A member shall not be auto, extern, or register.
6 The decl-specifier-seq can be omitted in constructor, destructor, and
conversion function declarations only. The member-declarator-list can
be omitted only after a class-specifier, an enum-specifier, or a decl-
specifier-seq of the form friend elaborated-type-specifier. A pure-
specifier shall be used only in the declaration of a virtual function
(_class.virtual_).
7 Non-static (_class.static_) members that are class objects shall be
objects of previously defined classes. In particular, a class cl
shall not contain an object of class cl, but it can contain a pointer
or reference to an object of class cl. When an array is used as the
type of a nonstatic member all dimensions shall be specified.
8 A simple example of a class definition is
struct tnode {
char tword[20];
int count;
tnode *left;
tnode *right;
};
which contains an array of twenty characters, an integer, and two
pointers to similar structures. Once this definition has been given,
the declaration
tnode s, *sp;
declares s to be a tnode and sp to be a pointer to a tnode. With
these declarations, sp->count refers to the count member of the struc
ture to which sp points; s.left refers to the left subtree pointer of
the structure s; and s.right->tword[0] refers to the initial character
of the tword member of the right subtree of s.
9 Nonstatic data members of a class declared without an intervening
access-specifier are allocated so that later members have higher
addresses within a class object. The order of allocation of nonstatic
data members separated by an access-specifier is implementation depen
dent (_class.access.spec_). Implementation alignment requirements
might cause two adjacent members not to be allocated immediately after
each other; so might requirements for space for managing virtual func
tions (_class.virtual_) and virtual base classes (_class.mi_); see
also _expr.cast_.
10A function member (_class.mfct_) with the same name as its class is a
constructor (_class.ctor_). A static data member, enumerator, member
of an anonymous union, or nested type shall not have the same name as
its class.
11Two POD-struct (_class_) types are layout-compatible if they have the
same number of members, and corresponding members (in order) have lay
out-compatible types (_basic.types_).
12Two POD-union (_class_) types are layout-compatible if they have the
same number of members, and corresponding members (in any order) have
layout-compatible types (_basic.types_).
+------- BEGIN BOX 2 -------+
Shouldn't this be the same set of types?
+------- END BOX 2 -------+
13If a POD-union contains several POD-structs that share a common ini
tial sequence, and if the POD-union object currently contains one of
these POD-structs, it is permitted to inspect the common initial part
of any of them. Two POD-structs share a common initial sequence if
corresponding members have layout-compatible types (and, for bit-
fields, the same widths) for a sequence of one or more initial mem
bers.
14A pointer to a POD-struct object, suitably converted, points to its
initial member (or if that member is a bit-field, then to the unit in
which it resides) and vice versa. There might therefore be unnamed
padding within a POD-struct object, but not at its beginning, as nec
essary to achieve appropriate alignment.
9.3 Scope rules for classes [class.scope0]
1 The following rules describe the scope of names declared in classes.
1)The scope of a name declared in a class consists not only of the
text following the name's declarator, but also of all function
bodies, default arguments, and constructor initializers in that
class (including such things in nested classes).
2)A name N used in a class S shal refer to the same declaration when
re-evaluated in its context and in the completed scope of S.
3)If reordering member declarations in a class yields an alternate
valid program under (1) and (2), the program's meaning is unde
fined.
4)A declaration in a nested declarative region hides a declaration
whose declarative region contains the nested declarative region.
5)A declaration within a member function hides a declaration whose
scope extends to or past the end of the member function's class.
6)The scope of a declaration that extends to or past the end of a
class definition also extends to the regions defined by its member
definitions, even if defined lexically outside the class (this
includes static data member initializations, nested class defini
tions and member function definitions (that is, the parameter-
declaration-clause including default arguments
(_dcl.fct.default_), the member function body and, for constructor
functions (_class.ctor_), the ctor-initializer
(_class.base.init_)).
2 For example:
typedef int c;
enum { i = 1 };
class X {
char v[i]; // error: 'i' refers to ::i
// but when reevaluated is X::i
int f() { return sizeof(c); } // okay: X::c
char c;
enum { i = 2 };
};
typedef char* T;
struct Y {
T a; // error: 'T' refers to ::T
// but when reevaluated is Y::T
typedef long T;
T b;
};
struct Z {
int f(const R); // error: 'R' is parameter name
// but swapping the two declarations
// changes it to a type
typedef int R;
};
9.4 Member functions [class.mfct]
1
+------- BEGIN BOX 3 -------+
This subclause does not take into account inheritance. Should it?
+------- END BOX 3 -------+
2 Functions declared in the definition of a class (excluding those
declared with a friend specifier; _class.friend_) are called member
functions of that class. A member function may be declared static
in which case it is a static member function of its class
(_class.static_); otherwise it is a nonstatic member function of its
class (_class.mfct.nonstatic_, _class.this_).
3 A member function may be defined (_dcl.fct.def_) in its class defi
nition, in which case it is an inline member function, or it may be
defined outside of its class definition if it has already been
declared but not defined in its class definition. This out-of-line
definition shall appear in a namespace scope containing the defini
tion of the member function's class.
4 An inline member function (whether static or nonstatic) may also be
defined outside of its class definition provided either its declara
tion in the class definition or its definition outside of the class
definition declares the function as inline, see _dcl.fct.spec_.
5 Member functions of a class in namespace scope have external link
age. Member functions of a local class (_class.local_) have no
linkage. See _basic.link_.
6 There shall be exactly one definition of a non-inline member func
tion in a program; no diagnostic is required. There may be more
than one inline member function definition in a program. See
_basic.def.odr_ and _dcl.fct.spec_.
7 If the definition of a member function is lexically outside its
class definition, the member function name shall be qualified by its
class name using the :: operator. A member function definition
(that is, the parameter-declaration-clause including the default
arguments (_dcl.fct.default_), the member function body and, for a
constructor function (_class.ctor_), the ctor-initializer
(_class.base.init_)) is in the scope of the member function's class
(_class.scope0). For example,
struct X {
typedef int T;
static T count;
void f(T);
};
void X::f(T t = count) { }
The member function f of class X is defined in global scope; the
notation X::f specifies that the function f is a member of class X
and in the scope of class X. In the function definition, the param
eter type T refers to the typedef member CW T declared in class X
and the default argument count refers to the static data member
count declared in class X.
8 A static local variable in a member function always refers to the
same object, whether or not the member function is inline.
9 Member functions may be mentioned in friend declarations after their
class has been defined.
10 Member functions of a local class shall be defined inline in their
class definition.
9.4.1 Nonstatic member functions [class.mfct.nonstatic]
1 A nonstatic member function may be called for an object of its class
type using the class member access syntax (_expr.ref_,
_over.match.call_). A nonstatic member function may also be called
directly from within the body of the member functions of its class
using the function call syntax (_expr.call_, _over.match.call_).
The effect of calling a nonstatic member function of a class X for
something that is not an object of class X is undefined.
2 The names of a member of class X may be used directly in the body of
a nonstatic member function of X. During name lookup, when an id-
expression (_expr.prim_) used in a nonstatic member function body
resolves to a nonstatic member of the member function's class, the
id-expression is transformed into a class member access expression
(_expr.ref_) using (*this) (_class.this_) as the postfix-expression
to the left of the . operator. The member name then refers to the
member of the object for which the function is called. Similarly
during name look up, when an unqualified-id (_expr.prim_) used in
the definition of a member function resolves to a static member, an
enumerator or a nested type of member function's class, the unquali
fied-id is transformed into a qualified-id (_expr.prim_) in which
the nested-name-specifier names the class of the member function.
For example,
struct tnode {
char tword[20];
int count;
tnode *left;
tnode *right;
void set(char*, tnode* l, tnode* r);
};
void tnode::set(char* w, tnode* l, tnode* r)
{
count = strlen(w)+1;
if (sizeof(tword)<=count)
error("tnode string too long");
strcpy(tword,w);
left = l;
right = r;
}
void f(tnode n1, tnode n2)
{
n1.set("abc",&n2,0);
n2.set("def",0,0);
}
The member names tword, count, left, and right refer to members of
the object for which the function is called. Thus, in the call
n1.set("abc",&n2,0), tword refers to n1.tword, and in the call
n2.set("def",0,0), it refers to n2.tword. The functions strlen,
error, and strcpy are not members of the class tnode and shall be
declared elsewhere.2)
3 The type of a nonstatic member function involves its class name;
thus the type of the qualified-id expression tnode::set is member
function and the type of &tnode::set is pointer to member function
(that is, void (tnode::*)(char*,tnote*,tnode*), see
_expr.unary.op_).
4 A nonstatic member function may be declared const, volatile, or
const volatile. These cv-qualifiers affect the type of the this
pointer, see _class.this_. They also affect the type of the member
function; a member function declared const is a const member func
tion, a member function declared volatile is a volatile member func
tion and a member function declared const volatile is a const
volatile member function. For example,
struct X {
void g() const;
void h() const volatile;
};
X::g is a const member function and X::h is a const volatile member
function.
5 A nonstatic member function may be declared virtual
(_class.virtual_) or pure virtual (_class.abstract_).
_________________________
2) See, for example, <cstring> (_lib.c.strings_).
9.4.2 The this pointer [class.this]
1 In the body of a nonstatic (_class.mfct_) member function, the key
word this is a non-lvalue expression whose value is the address of
the object for which the function is called. The type of this in a
member function of a class X is X*. If the member function is
declared const, the type of this is const X*, if the member function
is declared volatile, the type of this is volatile X*, and if the
member function is declared const volatile, the type of this is
const volatile X*.
2 In a const member function, the object for which the function is
called is accessed through a const access path; therefore, a const
member function shall not modify the object and its non-static mem
bers. For example,
struct s {
int a;
int f() const;
int g() { return a++; }
int h() const { return a++; } // error
};
int s::f() const { return a; }
The a++ in the body of s::h is ill-formed because it tries to modify
(a part of) the object for which s::h() is called. This is not
allowed in a const member function where this is a pointer to const,
that is, *this is a const.
3 Similarly, volatile semantics (_dcl.type.cv_) apply in volatile mem
ber functions when accessing the object and its non-static members.
4 A cv-qualified member function can be called on an object-expression
(_expr.ref_) only if the object-expression is as qualified or less-
qualified than the member function. For example,
void k(s& x, const s& y)
{
x.f();
x.g();
y.f();
y.g(); // error
}
The call y.g() is ill-formed because y is const and s::g() is a non-
const member function, that is, s::g() is less-qualified than the
object-expression y.
5 Constructors (_class.ctor_) and destructors (_class.dtor_) cannot be
declared const, volatile or const volatile; however, these functions
can be invoked to create and destroy objects with cv-qualified
types, see _class.ctor_ and _class.dtor_.
9.5 Static members [class.static]
1 A data or function member of a class may be declared static in a
class definition, in which case it is a static member of the class.
2 A static member s of class X may be referred to using the qualified-
id expression X::s; it is not necessary to use the class member
access syntax (_class.ref_) to refer to a static member. A static
member may be referred to using the class member access syntax, in
which case the object-expression is always evaluated. For example,
class process {
public:
static void reschedule();
};
process& g();
void f()
{
process::reschedule(); // ok: no object necessary
g().reschedule(); // g() is called
}
A static member may be referred to directly in the scope of its
class; in this case, the static member is referred to as if a quali
fied-id expression was used in which the nested-name-specifier names
the class of the static member. For example,
class X {
public:
static int i;
static int g();
};
int X::i = g(); // equivalent to X::g();
3 The definition of a static member function or the initializer
expression of a static data member definition may use the names of
the static members, enumerators, and nested types of the member's
class directly. During name lookup, when an unqualified-id
(_expr.prim_) used in the definition of a static member resolves to
a static member, enumerator or nested type of its class, the unqual
ified-id is transformed into a qualified-id expression in which the
nested-name-specifier names the class of the static member. The
definition of a static member shall not use directly the names of
the nonstatic members of its class (including as operands of the
sizeof operator). The definition of a static member may only refer
to the nonstatic members of its class by using the class member
access syntax (_expr.ref_) with an object-expression of its class
type.
4 Static members obey the usual class member access rules
(_class.access_).
5 The type of a static member does not involve its class name; thus,
in the example above, the type of the qualified-id expression X::g
is a function type and the type of &X::g is pointer to function type
(that is, void(*)(), see _expr.unary.op_).
9.5.1 Static member functions [class.static.mfct]
1 The rules described in _class.mfct_ apply to static member func
tions.
2 A static member function does not have a this pointer
(_class.this_). A static member function shall not be virtual.
There shall not be a static and a nonstatic member function with the
same name and the same parameter types (_over.load_). A nonstatic
member function shall not be declared const, volatile, const
volatile.
9.5.2 Static data members [class.static.data]
1 A static data member is not part of the subobjects of a class.
There is only one copy of a static data member shared by all the
objects of the class.
2 The declaration of a static data member in its class definition is
not a definition and may be of an incomplete type. A definition
shall be provided for the static data member in a namespace scope
enclosing the member's class definition. In the definition at
namespace scope, the name of the static data member shall be quali
fied by its class name using the :: operator. The initializer
expression in the definition of a static data member is in the scope
of its class (_class.scope0_). For example,
class process {
static process* run_chain;
static process* running;
};
process* process::running = get_main();
process* process::run_chain = running;
The static data member run_chain of class process is defined in
global scope; the notation process::run_chain specifies that the
member run_chain is a member of class process and in the scope of
class process. In the static data member definition, the initial
izer expression refers to the static data member running of class
process.
3 Once the static data member has been defined, it exists even if no
objects of its class have i been created. For example, in the exam
ple above, run_chain and running exist even if no objects of class
process are been created by the program.
4 If a static data member is of integral or enumeration type, its dec
laration in the class definition may specify a constant-initializer.
In that case, the member can appear in integral constant expressions
(_expr.const_) within its declarative region after its declaration.
The member shall still be defined in a namespace scope and the defi
nition of the member in namespace scope shall not contain an ini
tializer.
5 There shall be exactly one definition of a static data member in a
program; no diagnostic is required; see _basic.def.odr_.
6 Static data members of a class in namespace scope have external
linkage (_basic.link_). A local class cannot have static data mem
bers.
7 Static data members are initialized and destroyed exactly like
global objects; see _basic.start.init_ and _basic.start.term_.
8 A static data member cannot be mutable(_dcl.stc_).
9.6 Unions [class.union]
1 A union can be thought of as a class whose member objects all begin
at offset zero and whose size is sufficient to contain any of its
member objects. At most one of the member objects can be stored in
a union at any time. A union can have member functions (including
constructors and destructors), but not virtual (_class.virtual_)
functions. A union shall not have base classes. A union shall not
be used as a base class. An object of a class with a non-trivial
constructor (_class.ctor_) or a non-trivial destructor
(_class.dtor_) or with a user-defined copy assignment operator
(_over.ass_) cannot be a member of a union. A union can have no
static data members.
+------- BEGIN BOX 4 -------+
Shouldn't we prohibit references in unions?
+------- END BOX 4 -------+
2 A union of the form
union { member-specification } ;
is called an anonymous union; it defines an unnamed object (and not
a type). The names of the members of an anonymous union shall be
distinct from other names in the scope in which the union is
declared; they are used directly in that scope without the usual
member access syntax (_expr.ref_). For example,
void f()
{
union { int a; char* p; };
a = 1;
// ...
p = "Jennifer";
// ...
}
Here a and p are used like ordinary (nonmember) variables, but since
they are union members they have the same address.
3 A global anonymous union shall be declared static. An anonymous
union shall not have private or protected members (_class.access_).
An anonymous union shall not have function members.
4 A union for which objects or pointers are declared is not an anony
mous union. For example,
union { int aa; char* p; } obj, *ptr = &obj;
aa = 1; // error
ptr->aa = 1; // ok
The assignment to plain aa is ill formed since the member name is
not associated with any particular object.
5 Initialization of unions with no user-declared constructors is
described in _dcl.init.aggr_.
9.7 Bit-fields [class.bit]
1 A member-declarator of the form
identifieropt : constant-expression
specifies a bit-field; its length is set off from the bit-field name
by a colon. Allocation of bit-fields within a class object is
implementation dependent. Fields are packed into some addressable
allocation unit. Fields straddle allocation units on some machines
and not on others. Alignment of bit-fields is implementation depen
dent. Fields are assigned right-to-left on some machines, left-to-
right on others.
2 An unnamed bit-field is useful for padding to conform to externally-
imposed layouts. Unnamed fields are not members and cannot be ini
tialized. As a special case, an unnamed bit-field with a width of
zero specifies alignment of the next bit-field at an allocation unit
boundary.
3 A bit-field shall not be a static member. A bit-field shall have
integral or enumeration type (_basic.fundamental_). It is implemen
tation dependent whether a plain (neither explicitly signed nor
unsigned) int field is signed or unsigned. The address-of operator
& shall not be applied to a bit-field, so there are no pointers to
bit-fields. Nor are there references to bit-fields.
9.8 Nested class declarations [class.nest]
1 A class can be defined within another class. A class defined within
another is called a nested class. The name of a nested class is
local to its enclosing class. The nested class is in the scope of
its enclosing class. Except by using explicit pointers, references,
and object names, declarations in a nested class can use only type
names, static members, and enumerators from the enclosing class.
int x;
int y;
class enclose {
public:
int x;
static int s;
class inner {
void f(int i)
{
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 in scope
Member functions of a nested class have no special access to members
of an enclosing class; they obey the usual access rules
(_class.access_). Member functions of an enclosing class have no
special access to members of a nested class; they obey the usual
access rules. For example,
class E {
int x;
class I {
int y;
void f(E* p, int i)
{
p->x = i; // error: E::x is private
}
};
int g(I* p)
{
return p->y; // error: I::y is private
}
};
Member functions and static data members of a nested class can be
defined in a namespace scope containing the definition of their
class. For example,
class enclose {
public:
class inner {
static int x;
void f(int i);
};
};
int enclose::inner::x = 1;
void enclose::inner::f(int i) { /* ... */ }
A nested class Y may be declared in a class X and later defined in
the definition of class X or be later defined in a namespace scope
containing the definition of class X. For example:
class E {
class I1; // forward declaration of nested class
class I2;
class I1 {}; // definition of nested class
};
class E::I2 {}; // definition of nested class
Like a member function, a friend function (_class.friend_) defined
within a nested class is in the lexical scope of that class; it
obeys the same rules for name binding as a static member function of
that class (described in _class.static_) and has no special access
rights to members of an enclosing class.
9.9 Local class declarations [class.local]
1 A class can be defined within a function definition; such a class is
called a local class. The name of a local class is local to its
enclosing scope. The local class is in the scope of the enclosing
scope. Declarations in a local class can use only type names,
static variables, extern variables and functions, and enumerators
from the enclosing scope. For example,
int x;
void f()
{
static int s ;
int x;
extern int g();
struct local {
int g() { return x; } // error: `x' is auto
int h() { return s; } // ok
int k() { return ::x; } // ok
int l() { return g(); } // ok
};
// ...
}
local* p = 0; // error: `local' not in scope
2 An enclosing function has no special access to members of the local
class; it obeys the usual access rules (_class.access_). Member
functions of a local class shall be defined within their class defi
nition. A local class shall not have static data members.
9.10 Nested type names [class.nested.type]
1 Type names obey exactly the same scope rules as other names. In
particular, type names defined within a class definition cannot be
used outside their class without qualification. For example,
class X {
public:
typedef int I;
class Y { /* ... */ };
I a;
};
I b; // error
Y c; // error
X::Y d; // ok
X::I e; // ok