Reputation: 4214
The static keyword is related to internal linkage generally, but the static keyword used inside a class has external linkage right? The variables m, n below are accessible outside the class file.
class c {
int i;
int j;
static int m;
static int n;
public:
void zap();
static void clear();
};
Upvotes: 6
Views: 881
Reputation: 409482
You could say that static members are members of the class and not any specific object instance. That is, they have the same value for all object instances.
Static member functions, while not having a value, are otherwise the same. Instead of being unique for each object instance, they can be seen as part of the class. This means that they have no this
pointer and can not access non-static member variables.
Upvotes: 1
Reputation: 29656
As I stated in my comment, static
members are those associated only with the class rather than individual objects.
static
members belong to the class; for variables, they're accessible without an object and shared amongst instances e.g.struct Foo { static void *bar; static void *fu(); }
so
Foo::bar
andFoo::fu
are legal.
They are introduced in §9.4 of the C++03 standard;
A data or function member of a class may be declared
static
in a class definition, in which case it is a static member of the class.A
static
members
of classX
may be referred to using the qualified-id expressionX::s
; it is not necessary to use the class member access syntax (5.2.5) to refer to astatic
member. Astatic
member may be referred to using the class member access syntax, in which case the object-expression is evaluatedclass process { public: static void reschedule(); }; process& g(); void f() { process::reschedule(); // OK: no object necessary g().reschedule(); // g() is called }
A
static
member may be referred to directly in the scope of its class or in the scope of a class derived (clause 10) from its class; in this case, thestatic
member is referred to as if a qualified-id expression was used, with the nested-name-specifier of the qualified-id naming the class scope from which thestatic
member is referenced.int g(); struct X { static int g(); }; struct Y : X { static int i; }; int Y::i = g(); // equivalent to Y::g();
...
Upvotes: 2
Reputation: 171491
Right.
The keyword static
is heavily overloaded with too many different meanings:
Upvotes: 10