cppannotations/annotations/yo/classes/local.yo
2017-05-22 16:04:27 +02:00

37 lines
2.2 KiB
Text

Classes are usually defined at the global or namespace level. However, it is
entirely possible to define hi(class: local) a local class, i.e., inside a
function. Such classes are called hi(local class)em(local classes).
Local classes can be very useful in advanced applications involving
inheritance or templates (cf. section ref(NONDEFINIT)). At this point in the
annotations() they have limited use, although their main features can be
described. At the end of this section an example is provided.
itemization(
it() Local classes may use almost all characteristics of normal classes.
They may have constructors, destructors, data members, and member functions;
it() Local classes cannot define static data members. Static member
functions, however, em(can) be defined.
it() Since a local class may define static member functions, it is
possible to define emi(nested functions) in bf(C++) somewhat comparable to the
way programming languages like bi(Pascal) allow nested functions to be
defined.
it() If a local class needs access to a constant integral value, a
local em(enum) can be used. The tt(enum) may be anonymous, exposing only the
tt(enum) values.
it() Local classes cannot directly access the non-static variables of their
surrounding context. For example, in the example shown below the class
tt(Local) cannot directly access tt(main)'s tt(argc) parameter.
it() Local classes may directly access global data and static variables
defined by their surrounding function. This includes variables defined in the
anonymous namespace of the source file containing the local class.
it() Local class objects can be defined inside the function body, but they
cannot leave the function as objects of their own type. I.e., a local class
name cannot be used for either the return type or for the parameter types of
its surrounding function.
it() As a prelude to emi(inheritance) (chapter ref(INHERITANCE)):
a local class may be derived from an existing class allowing the surrounding
function to return a dynamically allocated locally constructed class object,
pointer or reference via a base class pointer or reference.
)
verbinclude(-a examples/local.cc)