Saturday, October 2, 2021

static keyword in C++

I studied this static class memeber for long time. then start to link it with static storage class concept. these two different usages cause different understanding:

  1. static members
  2. Inside a class definition, the keyword static declares members that are not bound to class instances. Outside a class definition, it has a different meaning: see storage duration..

  3. Storage class specifiers
  4. The storage class specifiers are a part of the decl-specifier-seq of a name's declaration syntax.

    Together with the scope of the name, they control two independent properties of the name: its storage duration and its linkage..

    • auto - automatic storage duration. (until C++11)
    • register - automatic storage duration. Also hints to the compiler to place the object in the processor's register. (deprecated) (until C++17)
    • static - static or thread storage duration and internal linkage.
    • extern - static or thread storage duration and external linkage.
    • thread_local - thread storage duration. (since C++11)
    • mutable - does not affect storage duration or linkage. See const/volatile for the explanation.

    Only one storage class specifier may appear in a declaration except that thread_local may be combined with static or with extern (since C++11).

  5. Scope
  6. Scope :Each name that appears in a C++ program is only visible in some possibly discontiguous portion of the source code called its scope.

    Within a scope, unqualified name lookup can be used to associate the name with its declaration...

  7. C++ keywords: static
  8. Usage:

    1. declarations of namespace members with static storage duration and internal linkage
    2. definitions of block scope variables with static storage duration and initialized once
    3. declarations of class members not bound to specific instances.

No comments:

Post a Comment