cppannotations/yo/nested/declaring.yo
2009-12-24 12:38:17 +00:00

30 lines
1.2 KiB
Text

Nested classes hi(nested class: declaration) may be declared before they are
actually defined in a surrounding class. Such forward declarations are
required if a class contains multiple nested classes, and the nested classes
contain pointers, references, parameters or return values to objects of the
other nested classes.
For example, the following class tt(Outer) contains two nested classes
tt(Inner1) and tt(Inner2). The class tt(Inner1) contains a pointer to
tt(Inner2) objects, and tt(Inner2) contains a pointer to tt(Inner1)
objects. Cross references require forward declarations. Forward declarations
must be given an access specification that is identical to the access
specification of their definitions. In the following example the tt(Inner2)
forward declaration must be given in a tt(private) section, as its definition
is also part of the class tt(Outer)'s private interface:
verb(
class Outer
{
private:
class Inner2; // forward declaration
class Inner1
{
Inner2 *pi2; // points to Inner2 objects
};
class Inner2
{
Inner1 *pi1; // points to Inner1 objects
};
};
)