static

suggest change

The static storage class specifier has three different meanings.

  1. Gives internal linkage to a variable or function declared at namespace scope.
    // internal function; can't be linked to
    static double semiperimeter(double a, double b, double c) {
        return (a + b + c)/2.0;
    }
    // exported to client
    double area(double a, double b, double c) {
        const double s = semiperimeter(a, b, c);
        return sqrt(s*(s-a)*(s-b)*(s-c));
    }
  2. Declares a variable to have static storage duration (unless it is thread_local). Namespace-scope variables are implicitly static. A static local variable is initialized only once, the first time control passes through its definition, and is not destroyed every time its scope is exited.
    void f() {
        static int count = 0;
        std::cout << "f has been called " << ++count << " times so far\n";
    }
  3. When applied to the declaration of a class member, declares that member to be a static member.
    struct S {
        static S* create() {
            return new S;
        }
    };
    int main() {
        S* s = S::create();
    }

    Note that in the case of a static data member of a class, both 2 and 3 apply simultaneously: the static keyword both makes the member into a static data member and makes it into a variable with static storage duration.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


keywords:
* class
* float
* bool
* void
* char
* short
* long
* double
* int
* enum
* if
* do
* else
* true
* false
* const
* for
* goto
* struct
* case
* catch
* return
* signed
* static
* this
* switch
* throw
* try
* break
* union
* asm
* sizeof

Table Of Contents
8 Arrays
11 Loops
14 keywords
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype