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();
};
Right.
The keyword static is heavily overloaded with too many different meanings:
As I stated in my comment, static members are those associated only with the class rather than individual objects.
staticmembers 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::barandFoo::fuare legal.
They are introduced in §9.4 of the C++03 standard;
A data or function member of a class may be declared
staticin a class definition, in which case it is a static member of the class.A
staticmembersof classXmay 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 astaticmember. Astaticmember 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
staticmember 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, thestaticmember 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 thestaticmember is referenced.int g(); struct X { static int g(); }; struct Y : X { static int i; }; int Y::i = g(); // equivalent to Y::g();...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With