2009-12-12 16:28:53 +01:00
|
|
|
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.
|
2006-09-04 10:26:34 +02:00
|
|
|
|
2009-12-12 16:28:53 +01:00
|
|
|
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
|
2006-09-04 10:26:34 +02:00
|
|
|
are. See the next section (ref(CALLINGCONVENTION)) for a subtle note). Since
|
2007-04-11 13:40:27 +02:00
|
|
|
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:
|
2006-09-04 10:26:34 +02:00
|
|
|
verb(
|
|
|
|
class Directory
|
|
|
|
{
|
|
|
|
string d_currentPath;
|
|
|
|
static char s_path[];
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void setpath(char const *newpath);
|
2010-08-25 15:50:41 +02:00
|
|
|
static void preset(Directory &dir, char const *newpath);
|
2006-09-04 10:26:34 +02:00
|
|
|
};
|
2006-11-19 21:17:14 +01:00
|
|
|
inline void Directory::preset(Directory &dir, char const *newpath)
|
|
|
|
{
|
2006-09-04 10:26:34 +02:00
|
|
|
// see the text below
|
2006-11-19 21:17:14 +01:00
|
|
|
dir.d_currentPath = newpath; // 1
|
|
|
|
}
|
|
|
|
|
|
|
|
char Directory::s_path[200] = "/usr/local"; // 2
|
2006-09-04 10:26:34 +02:00
|
|
|
|
|
|
|
void Directory::setpath(char const *newpath)
|
|
|
|
{
|
|
|
|
if (strlen(newpath) >= 200)
|
|
|
|
throw "newpath too long";
|
|
|
|
|
2006-11-19 21:17:14 +01:00
|
|
|
strcpy(s_path, newpath); // 3
|
2006-09-04 10:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2009-12-12 16:28:53 +01:00
|
|
|
it() at 1 a static member function modifies a private data member
|
2006-11-19 21:17:14 +01:00
|
|
|
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)
|
2010-02-28 17:34:35 +01:00
|
|
|
static member functions can be defined as inline
|
2009-12-12 16:28:53 +01:00
|
|
|
hi(inline: static members) functions.
|
2006-11-19 21:17:14 +01:00
|
|
|
it() at 2 a relatively long array is defined to be able to accomodate
|
2006-09-04 10:26:34 +02:00
|
|
|
long paths. Alternatively, a tt(string) or a pointer to dynamic memory could
|
2009-12-12 16:28:53 +01:00
|
|
|
be used.
|
2006-11-19 21:17:14 +01:00
|
|
|
it() at 3 a (possibly longer, but not too long) new pathname is stored in
|
2009-12-12 16:28:53 +01:00
|
|
|
the static data member tt(s_path[]). Note that only static members are used.
|
2006-09-04 10:26:34 +02:00
|
|
|
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
|
2009-12-12 16:28:53 +01:00
|
|
|
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
|
2006-09-04 10:26:34 +02:00
|
|
|
the compiler that we're talking about a function in the tt(Directory)
|
2009-12-12 16:28:53 +01:00
|
|
|
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
|
2010-02-28 17:34:35 +01:00
|
|
|
syntax.
|
2009-12-12 16:28:53 +01:00
|
|
|
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)
|
2006-09-04 10:26:34 +02:00
|
|
|
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
|
2009-12-12 16:28:53 +01:00
|
|
|
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.
|
2006-09-04 10:26:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
In the example only public static member functions were used. bf(C++)
|
2009-12-12 16:28:53 +01:00
|
|
|
also allows the definition of private static member functions. Such functions
|
2006-09-04 10:26:34 +02:00
|
|
|
can only be called by member functions of their class.
|