cppannotations/annotations/yo/static/function.yo

92 lines
4.1 KiB
Text
Raw Normal View History

In addition to static data members, bf(C++) allows us to define
emi(static member functions). Similar to static data that are shared by
all objects of the class, static member functions also exist without any
associated object of their class.
Static member functions hi(static member functions) can access all static
members of their class, but em(also) the members (tt(private) or tt(public))
of objects of their class em(if) they are informed about the existence of
these objects (as in the upcoming example). As static member functions are not
associated with any object of their class they do not have a
ti(this) pointer. In fact, a static member function is completely comparable
to a i(global function), not associated with any class (i.e., in practice they
are. See the next section (ref(CALLINGCONVENTION)) for a subtle note). Since
static member functions do not require an associated object, static member
functions declared in the public section of a class interface may be called
without specifying an object of its class. The following example illustrates
this characteristic of static member functions:
verb(
class Directory
{
string d_currentPath;
static char s_path[];
public:
static void setpath(char const *newpath);
static void preset(Directory &dir, char const *newpath);
};
inline void Directory::preset(Directory &dir, char const *newpath)
{
// see the text below
dir.d_currentPath = newpath; // 1
}
char Directory::s_path[200] = "/usr/local"; // 2
void Directory::setpath(char const *newpath)
{
if (strlen(newpath) >= 200)
throw "newpath too long";
strcpy(s_path, newpath); // 3
}
int main()
{
Directory dir;
Directory::setpath("/etc"); // 4
dir.setpath("/etc"); // 5
Directory::preset(dir, "/usr/local/bin"); // 6
dir.preset(dir, "/usr/local/bin"); // 7
}
)
itemization(
it() at 1 a static member function modifies a private data member
of an object. However, the object whose member must be modified is given to
the member function as a reference parameter.
Note that hi(static inline member functions)
static member functions can be defined as inline
hi(inline: static members) functions.
it() at 2 a relatively long array is defined to be able to accomodate
long paths. Alternatively, a tt(string) or a pointer to dynamic memory could
be used.
it() at 3 a (possibly longer, but not too long) new pathname is stored in
the static data member tt(s_path[]). Note that only static members are used.
it() at 4, tt(setpath()) is called. It is a static member, so no
object is required. But the compiler must know to which class the function
belongs, so the class is mentioned using the scope resolution operator.
it() at 5, the same is implemented as in 4. Here tt(dir) is used to tell
the compiler that we're talking about a function in the tt(Directory)
class. Static member functions em(can) be called as normal member
functions, but this does not imply that the static member function receives
the object's address as a tt(this) pointer. Here the member-call syntax is
used as an alternative for the classname plus scope resolution operator
syntax.
it() at 6, tt(currentPath) is altered. As in 4, the class and the scope
resolution operator are used.
it() at 7, the same is implemented as in 6. But here tt(dir) is used to
tell the compiler that we're talking about a function in the tt(Directory)
class. Here in particular note that this is em(not) using tt(preset()) as an
ordinary member function of tt(dir): the function still has no
ti(this)-pointer, so tt(dir) must be passed as argument to inform the static
member function tt(preset) about the object whose tt(currentPath) member it
should modify.
)
In the example only public static member functions were used. bf(C++)
also allows the definition of private static member functions. Such functions
can only be called by member functions of their class.