欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

理解static关键字 In C++:第一重

程序员文章站 2024-02-29 17:22:46
...
  • Static Keyword in C++

Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static keyword can be used with following:

  1. Static variable in functions

    Static variables when used inside function are initialized only once, and then they hold there value even through function calls.

    These static variables are stored on static storage area, not in stack.(《理解static storage area VS. stack In C++》)

    void counter()
    {
        static int count = 0;
        cout << count++;
    }
    int main ()
    {
        for (int i=0; i<5; i++)
        {
            counter();
        }
    }
    >>> output
    0 1 2 3 4
    

    Same program output without using static variable :

    void counter(){
        int count = 0;
        cout << count++;
    }
    int main()
    {
        for (int i=0; i<5; i++)
        {
            counter();
        }
    }
    >>> output
    0 0 0 0 0 
    

    If we don’t use static keyword, the variable count, is reinitialized everytime when counter() function is called.

    If you don’t initialize a static variable, they are by default initialized to zero.

  2. Static Class Objects

    Static keyword works in the same way for class objects too. Objects declared static are allocated storage in static storage area, and have scope till the end of program.

    Static objects are also initialized using constructors like other normal objects. Assignment to zero, on using static keyword is only for primitive datatypes, not for user defined datatypes.

  3. Static member Variable in class

    Static data members of class are those members which are shared by all objects.

    Static data member has a single piece of storage, and is not avaiable as separate copy with each object, like other non-static data members.

    Static member variables (data members) are not initialied using constructor, because these are not dependent on object initialization.

    Also, it must be initialized explicitly, always outside the class. If not initialized, Linker will give error.

  4. Static Methods in class

    These functions work for the class as whole rather than for a particular object of a class.

    It can be called using an object and the direct member access . operator. But , its more typical to call a static member function by itself, using class name and scope resolution :: operator.

    A static member function differs from a regular member function in that it can be called without an instance of a class, and since it has no instance, it cannot access non-static members of the class.

    static is often used as a class member function, and only very rarely used for a free-standing function.

    A static free-function means that the function will not be referred to by any other translation unit, and thus the linker can ignore it entirely. This has a small number of purposes :

    • Can be used in a cpp file to guarantee that the function is never used from any other file
    • Can be put in a header and every file will have it’s own copy of the function. Not useful, since inline does pretty much the same thing.
    • Speeds up link time by reducing work
    • Can put a function with the same name in each translation unit, and they can all do different things.
  • Another view

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static function can be called directly by using class name.

Static variables are initialized only once. Compiler persist the variable till the end of the program.

Static variable can be defined inside or outside the function. They are local to the block.

The default value of static variable is zero.

The static variables are alive till the execution of the program.

  • References

  1. Studytonight
  2. The static keyword and its various uses in C++
  3. tutorialspoint