The static storage class specifier
Objects declared with the static storage class specifier have static storage duration, which means that memory for these objects is allocated when the program begins running and is freed when the program terminates. Static storage duration for a variable is different from file or global scope: a variable can have static duration but local scope.
The keyword static is the major mechanism in C to enforce information hiding.
The static storage class specifier can be applied to the following declarations:
You cannot use the static storage class specifier with the following:
Linkage of static variables
A declaration of an object that contains the static storage class specifier and has file scope gives the identifier internal linkage. Each instance of the particular identifier therefore represents the same object within one file only. For example, if a static variable x has been declared in function f, when the program exits the scope of f, x is not destroyed:
#include <stdio.h>
int f(void) {
static int x = 0;
x++;
return x;
}
int main(void) {
int j;
for (j = 0; j < 5; j++) {
printf("Value of f(): %d\n", f());
}
return 0;
}
The following is the output of the above example:
Value of f(): 1
Value of f(): 2
Value of f(): 3
Value of f(): 4
Value of f(): 5
Because x is a static variable, it is not reinitialized to 0 on successive calls to f.
Static members
Class members can be declared using the storage class specifier static in the class member list. Only one copy of the static member is shared by all objects of a class in a program. When you declare an object of a class having a static member, the static member is not part of the class object.
struct X {
static int f();
};
int main() {
X::f();
}
Using the class access operators with static members
#include <iostream>
using namespace std;
struct A {
static void f() { cout << "In static function A::f()" << endl; }
};
int main() {
// no object required for static member
A::f();
A a;
A* ap = &a;
a.f();
ap->f();
}
The three statements A::f(), a.f(), and ap->f() all call the same static member function A::f().
#include <iostream>
using namespace std;
int g() {
cout << "In function g()" << endl;
return 0;
}
class X {
public:
static int g() {
cout << "In static member function X::g()" << endl;
return 1;
}
};
class Y: public X {
public:
static int i;
};
int Y::i = g();
int main() { }
The following is the output of the above code:
In static member function X::g()
The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope. For example:
int X::i = 0; // definition outside class declaration
int C::i = C::f(); // initialize with static member function
int C::j = C::i; // initialize with another static data member
int C::k = c.f(); // initialize with member function from an object
int C::l = c.j; // initialize with data member from an object
int C::s = c.a; // initialize with nonstatic data member
int C::r = 1; // initialize with a constant value
The tokens = 76 at the end of the declaration of static data member a is a constant initializer.
Static member functions
You cannot have static and nonstatic member functions with the same names and the same number and type of arguments.
A static member function does not have a this pointer. The following example demonstrates this:
#include <iostream>
using namespace std;
struct X {
private:
int i;
static int si;
public:
void set_i(int arg) { i = arg; }
static void set_si(int arg) { si = arg; }
void print_i() {
cout << "Value of i = " << i << endl;
cout << "Again, value of i = " << this->i << endl;
}
static void print_si() {
cout << "Value of si = " << si << endl;
// cout << "Again, value of si = " << this->si << endl;
}
};
int X::si = 77; // Initialize static data member
int main() {
X xobj;
xobj.set_i(11);
xobj.print_i();
// static data members and functions belong to the class and
// can be accessed without using an object of class X
X::print_si();
X::set_si(22);
X::print_si();
}
The following is the output of the above example:
Value of i = 11
Again, value of i = 11
Value of si = 77
Value of si = 22
#include <iostream>
using namespace std;
class C {
static void f() {
cout << "Here is i: " << i << endl;
}
static int i;
int j;
public:
C(int firstj): j(firstj) { }
void printall();
};
void C::printall() {
cout << "Here is j: " << this->j << endl;
this->f();
}
int C::i = 3;
int main() {
C obj_C(0);
obj_C.printall();
}
The following is the output of the above example:
Here is j: 0
Here is i: 3
A static member function cannot be declared with the keywords virtual, const, volatile, or const volatile.
No comments:
Post a Comment