What is the difference between a static and const variable?

前端 未结 17 1752
天命终不由人
天命终不由人 2020-11-29 18:39

Can someone explain the difference between a static and const variable?

相关标签:
17条回答
  • 2020-11-29 18:45

    Constants can't be changed, static variables have more to do with how they are allocated and where they are accessible.

    Check out this site.

    0 讨论(0)
  • 2020-11-29 18:45

    Constant variables cannot be changed. Static variable are private to the file and only accessible within the program code and not to anyone else.

    0 讨论(0)
  • 2020-11-29 18:46

    static means local for compilation unit (i.e. a single C++ source code file), or in other words it means it is not added to a global namespace. you can have multiple static variables in different c++ source code files with the same name and no name conflicts.

    const is just constant, meaning can't be modified.

    0 讨论(0)
  • 2020-11-29 18:47

    static

    is used for making the variable a class variable. You need not define a static variable while declaring.

    Example:

    #include <iostream>
    
    class dummy
    {
            public:
                    static int dum;
    };
    
    
    int dummy::dum = 0; //This is important for static variable, otherwise you'd get a linking error
    
    int main()
    {
            dummy d;
            d.dum = 1;
    
            std::cout<<"Printing dum from object: "<<d.dum<<std::endl;
            std::cout<<"Printing dum from class: "<<dummy::dum<<std::endl;
    
            return 0;
    }
    

    This would print: Printing dum from object: 1 Printing dum from class: 1

    The variable dum is a class variable. Trying to access it via an object just informs the compiler that it is a variable of which class. Consider a scenario where you could use a variable to count the number of objects created. static would come in handy there.

    const

    is used to make it a read-only variable. You need to define and declare the const variable at once.

    In the same program mentioned above, let's make the dum a const as well:

    class dummy
    {
            public:
                    static const int dum; // This would give an error. You need to define it as well
                    static const int dum = 1; //this is correct
                    const int dum = 1; //Correct. Just not making it a class variable
    
    };
    

    Suppose in the main, I am doing this:

    int main()
    {
            dummy d;
            d.dum = 1; //Illegal!
    
            std::cout<<"Printing dum from object: "<<d.dum<<std::endl;
            std::cout<<"Printing dum from class: "<<dummy::dum<<std::endl;
    
            return 0;
    }
    

    Though static has been manageable to understand, const is messed up in c++. The following resource helps in understanding it better: http://duramecho.com/ComputerInformation/WhyHowCppConst.html

    0 讨论(0)
  • 2020-11-29 18:48

    Const means “cannot be changed.”

    Static means “static instance (in memory) vs dynamic instance (on the stack.)” Static variables exist for the duration of the program. Dynamic ones are created and destroyed as needed.

    A variable can be one or both.

    0 讨论(0)
  • 2020-11-29 18:49

    A constant value cannot change. A static variable exists to a function, or class, rather than an instance or object.

    These two concepts are not mutually exclusive, and can be used together.

    0 讨论(0)
提交回复
热议问题