cppannotations/annotations/yo/static/intro.yo
Frank B. Brokken 777b182edd Moved all files but 'excluded', 'sf', and 'sourcetar' to ./annotations
This allowed me to standardize the sourcetar and sf/* scripts: the base
    directory (containing ./git) is now empty, except for maintenance scripts,
    while the source files and build scripts of the annotations are stored in
    a subdirectory of their own.
2013-05-29 20:44:08 +02:00

31 lines
1.7 KiB
Text

In the previous chapters we provided examples of classes where each object
had its own set of data members data. Each of the class's
member functions could access any member of any object of its class.
In some situations it may be desirable to define
emi(common data fields), that may be accessed by em(all) objects of the
class. For example, the name of the startup directory, used by a program that
recursively scans the directory tree of a disk. A second example is a variable
that indicates whether some specific initialization has occurred. In that case
the object that was constructed first would perform the initialization and
would set the flag to `done'.
Such situations are also encountered in bf(C), where several functions need to
access the same variable. A common solution in bf(C) is to define all these
functions in one source file and to define the variable ti(static): the
variable name is invisible outside the scope of the source file. This approach
is quite valid, but violates our philosophy of using only one function per
source file. Another bf(C)-solution is to give the variable in question an
unusual name, e.g., tt(_6uldv8), hoping that other program parts won't use
this name by accident. Neither the first, nor the second legacy bf(C) solution
is elegant.
bf(C++) solves the problem by defining ti(static members): data and functions,
common to all objects of a class and (when defined in the private section)
inaccessible outside of the class. These static members are this chapter's
topic.
Static members cannot be defined as hi(virtual: vs static) virtual
functions. A virtual member function is an ordinary member in that it has a
ti(this) pointer. As static member functions have no tt(this) pointer, they
cannot be declared virtual.