The static keyword and its various uses in C++

后端 未结 9 1714
自闭症患者
自闭症患者 2020-11-22 02:07

The keyword static is one which has several meanings in C++ that I find very confusing and I can never bend my mind around how its actually supposed to work.

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 02:41

    Static variables are shared between every instance of a class, instead of each class having their own variable.

    class MyClass
    {
        public:
        int myVar; 
        static int myStaticVar;
    };
    
    //Static member variables must be initialized. Unless you're using C++11, or it's an integer type,
    //they have to be defined and initialized outside of the class like this:
    MyClass::myStaticVar = 0;
    
    MyClass classA;
    MyClass classB;
    

    Each instance of 'MyClass' has their own 'myVar', but share the same 'myStaticVar'. In fact, you don't even need an instance of MyClass to access 'myStaticVar', and you can access it outside of the class like this:

    MyClass::myStaticVar //Assuming it's publicly accessible.
    

    When used inside a function as a local variable (and not as a class member-variable) the static keyword does something different. It allows you to create a persistent variable, without giving global scope.

    int myFunc()
    {
       int myVar = 0; //Each time the code reaches here, a new variable called 'myVar' is initialized.
       myVar++;
    
       //Given the above code, this will *always* print '1'.
       std::cout << myVar << std::endl;
    
       //The first time the code reaches here, 'myStaticVar' is initialized. But ONLY the first time.
       static int myStaticVar = 0;
    
       //Each time the code reaches here, myStaticVar is incremented.
       myStaticVar++;
    
       //This will print a continuously incrementing number,
       //each time the function is called. '1', '2', '3', etc...
       std::cout << myStaticVar << std::endl;
    }
    

    It's a global variable in terms of persistence... but without being global in scope/accessibility.

    You can also have static member functions. Static functions are basically non-member functions, but inside the class name's namespace, and with private access to the class's members.

    class MyClass
    {
        public:
        int Func()
        {
            //...do something...
        }
    
        static int StaticFunc()
        {
            //...do something...
        }
    };
    
    int main()
    {
       MyClass myClassA;
       myClassA.Func(); //Calls 'Func'.
       myClassA.StaticFunc(); //Calls 'StaticFunc'.
    
       MyClass::StaticFunc(); //Calls 'StaticFunc'.
       MyClass::Func(); //Error: You can't call a non-static member-function without a class instance!
    
       return 0;
    }
    

    When you call a member-function, there's a hidden parameter called 'this', that is a pointer to the instance of the class calling the function. Static member functions don't have that hidden parameter... they are callable without a class instance, but also cannot access non-static member variables of a class, because they don't have a 'this' pointer to work with. They aren't being called on any specific class instance.

提交回复
热议问题