Static class member variable and static variable in c++












0















What are the major differences between the static class member variable and the static variable ?
Both static class members and static varibles are accessed from member functions of any class. What are the specific uses of static class members and static varibles?










share|improve this question























  • The static class member of one class can be accessed by the member function of other class. Same is applicale to ordinary static variable. What factor distinguishes these two types of variables.

    – AvadhanaSolutions
    May 5 '15 at 7:26











  • static class member variable - one copy for all objects and static variable - does not destroyed even after it is out of scope, it is maintained through out the program running time

    – pola sai ram
    May 5 '15 at 7:27











  • Only one copy of static class member is created and shared by all objects

    – AvadhanaSolutions
    May 5 '15 at 7:40
















0















What are the major differences between the static class member variable and the static variable ?
Both static class members and static varibles are accessed from member functions of any class. What are the specific uses of static class members and static varibles?










share|improve this question























  • The static class member of one class can be accessed by the member function of other class. Same is applicale to ordinary static variable. What factor distinguishes these two types of variables.

    – AvadhanaSolutions
    May 5 '15 at 7:26











  • static class member variable - one copy for all objects and static variable - does not destroyed even after it is out of scope, it is maintained through out the program running time

    – pola sai ram
    May 5 '15 at 7:27











  • Only one copy of static class member is created and shared by all objects

    – AvadhanaSolutions
    May 5 '15 at 7:40














0












0








0








What are the major differences between the static class member variable and the static variable ?
Both static class members and static varibles are accessed from member functions of any class. What are the specific uses of static class members and static varibles?










share|improve this question














What are the major differences between the static class member variable and the static variable ?
Both static class members and static varibles are accessed from member functions of any class. What are the specific uses of static class members and static varibles?







c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 5 '15 at 7:20









AvadhanaSolutionsAvadhanaSolutions

16713




16713













  • The static class member of one class can be accessed by the member function of other class. Same is applicale to ordinary static variable. What factor distinguishes these two types of variables.

    – AvadhanaSolutions
    May 5 '15 at 7:26











  • static class member variable - one copy for all objects and static variable - does not destroyed even after it is out of scope, it is maintained through out the program running time

    – pola sai ram
    May 5 '15 at 7:27











  • Only one copy of static class member is created and shared by all objects

    – AvadhanaSolutions
    May 5 '15 at 7:40



















  • The static class member of one class can be accessed by the member function of other class. Same is applicale to ordinary static variable. What factor distinguishes these two types of variables.

    – AvadhanaSolutions
    May 5 '15 at 7:26











  • static class member variable - one copy for all objects and static variable - does not destroyed even after it is out of scope, it is maintained through out the program running time

    – pola sai ram
    May 5 '15 at 7:27











  • Only one copy of static class member is created and shared by all objects

    – AvadhanaSolutions
    May 5 '15 at 7:40

















The static class member of one class can be accessed by the member function of other class. Same is applicale to ordinary static variable. What factor distinguishes these two types of variables.

– AvadhanaSolutions
May 5 '15 at 7:26





The static class member of one class can be accessed by the member function of other class. Same is applicale to ordinary static variable. What factor distinguishes these two types of variables.

– AvadhanaSolutions
May 5 '15 at 7:26













static class member variable - one copy for all objects and static variable - does not destroyed even after it is out of scope, it is maintained through out the program running time

– pola sai ram
May 5 '15 at 7:27





static class member variable - one copy for all objects and static variable - does not destroyed even after it is out of scope, it is maintained through out the program running time

– pola sai ram
May 5 '15 at 7:27













Only one copy of static class member is created and shared by all objects

– AvadhanaSolutions
May 5 '15 at 7:40





Only one copy of static class member is created and shared by all objects

– AvadhanaSolutions
May 5 '15 at 7:40












5 Answers
5






active

oldest

votes


















4














The only reason is code cleanliness. You cannot restrict access to a global static variable like



static int globalValue=5;


it is (at least) visible in the source file you defined it.
With a class static, you can give a user of your class hints, how you wish to access it or be accessed. It is only visible within the class scope:



class myGlobalContainer
{
public:
static int myInt;
protected:
static float myFloat;
private:
static bool myBool;
};


the access of myInt is done by:



int x=myGlobalContainer::myInt;


the public modifier gives the user the hint that you see this value as part of the myGlobalContainer and wish him to use it. You do not polute the global namespace like you do with the globalValue.



The modifier protected and private shows that you do not wish that an "outsider" access those values.



protected and private static attributes are mostly used to share information between the instances of a class, for e.g. a instance counter:



class myGlobalContainer
{
public:
myGlobalContainer()
{
if(counter==0)
DoSomeSpecialGlobalInit();
counter++;
}
~myGlobalContainer()
{
counter--;
if(counter==0)
DoSomeSpecialGlobalUnInit();
}
private:
static int counter=0;
};


public static attributes are often seen with const. They mostly give a user a shortcut. For e.g.:



COLOR white=COLOR::WHITE;


instead of:



COLOR white=COLOR::FromAGBR(255,255,255,255);


Add least:
If you should use statics or not is a complete other discussion.






share|improve this answer

































    1















    Both static class members and static varibles are accessed from member
    functions of any class.




    That is not true:



    class A {
    private:
    static int x;
    };
    int A::x = 5;

    class B {
    static int y;
    public:
    void do_something()
    {
    std::cout << A::x; // Can't access A::x because it's private
    }
    };
    int B::y = 10;


    Although, if we did this:



    static int J = 9;

    class A {
    private:
    static int x;
    };
    int A::x = 5;

    class B {
    static int y;
    public:
    void do_something()
    {
    std::cout << J; // Yes, J is global.
    }
    };
    int B::y = 10;



    • Static member variables can access the private section of it's class opposed to a normal static variable.

    • Static member variables may not be defined inside the class body, unless it's const static or constexpr static.

    • Static member variables may be used as default arguments for the member functions in their class. Opposed to normal static variables, unless they are global.


    Uses: if you want a variable to be alive until the end of your program in both cases but static member variables have access to the private section of that class.






    share|improve this answer


























    • Correct. But public static class members can be accessed by member functions of any other class.

      – AvadhanaSolutions
      May 5 '15 at 10:02



















    0














    We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.



    If you declare a static member public then you can access it without member functions as well.. Static has nothing to do with the scope of variable.. It specifies the storage duration only..



    A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.






    share|improve this answer
























    • How is it different than ordinary non-class static variable?

      – AvadhanaSolutions
      May 5 '15 at 7:29











    • When we declarea member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. Which is not true with non-static class members.. Different copies of non-static variables are created for each different object

      – anshabhi
      May 5 '15 at 8:03











    • static members are used when you have to share some data with all instances of a class.. Suppose, you are designing a video game.. u have 2 heros and 100 enemies.. Then, 100 enemies is declared as static member since both heroes have to use this information..

      – anshabhi
      May 5 '15 at 8:09



















    0














    My list of differences:




    1. You can make static class member protected or private.

    2. You can't make static class member global.


    Can't think of anything else.






    share|improve this answer































      0














      Static class members are used to share data between different instances of a class. The storage for these members is allocated only once and it is only this instance of the static member that will be available for all objects of the class.



      Static variables inside a function are variables that retain its value through function calls.
      A classic but simple example of this is a int type static counter in a function where you want to keep track of the number of times this function was called. Since this retains its value through calls, you can increment it inside the function every time it is called.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30046555%2fstatic-class-member-variable-and-static-variable-in-c%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        The only reason is code cleanliness. You cannot restrict access to a global static variable like



        static int globalValue=5;


        it is (at least) visible in the source file you defined it.
        With a class static, you can give a user of your class hints, how you wish to access it or be accessed. It is only visible within the class scope:



        class myGlobalContainer
        {
        public:
        static int myInt;
        protected:
        static float myFloat;
        private:
        static bool myBool;
        };


        the access of myInt is done by:



        int x=myGlobalContainer::myInt;


        the public modifier gives the user the hint that you see this value as part of the myGlobalContainer and wish him to use it. You do not polute the global namespace like you do with the globalValue.



        The modifier protected and private shows that you do not wish that an "outsider" access those values.



        protected and private static attributes are mostly used to share information between the instances of a class, for e.g. a instance counter:



        class myGlobalContainer
        {
        public:
        myGlobalContainer()
        {
        if(counter==0)
        DoSomeSpecialGlobalInit();
        counter++;
        }
        ~myGlobalContainer()
        {
        counter--;
        if(counter==0)
        DoSomeSpecialGlobalUnInit();
        }
        private:
        static int counter=0;
        };


        public static attributes are often seen with const. They mostly give a user a shortcut. For e.g.:



        COLOR white=COLOR::WHITE;


        instead of:



        COLOR white=COLOR::FromAGBR(255,255,255,255);


        Add least:
        If you should use statics or not is a complete other discussion.






        share|improve this answer






























          4














          The only reason is code cleanliness. You cannot restrict access to a global static variable like



          static int globalValue=5;


          it is (at least) visible in the source file you defined it.
          With a class static, you can give a user of your class hints, how you wish to access it or be accessed. It is only visible within the class scope:



          class myGlobalContainer
          {
          public:
          static int myInt;
          protected:
          static float myFloat;
          private:
          static bool myBool;
          };


          the access of myInt is done by:



          int x=myGlobalContainer::myInt;


          the public modifier gives the user the hint that you see this value as part of the myGlobalContainer and wish him to use it. You do not polute the global namespace like you do with the globalValue.



          The modifier protected and private shows that you do not wish that an "outsider" access those values.



          protected and private static attributes are mostly used to share information between the instances of a class, for e.g. a instance counter:



          class myGlobalContainer
          {
          public:
          myGlobalContainer()
          {
          if(counter==0)
          DoSomeSpecialGlobalInit();
          counter++;
          }
          ~myGlobalContainer()
          {
          counter--;
          if(counter==0)
          DoSomeSpecialGlobalUnInit();
          }
          private:
          static int counter=0;
          };


          public static attributes are often seen with const. They mostly give a user a shortcut. For e.g.:



          COLOR white=COLOR::WHITE;


          instead of:



          COLOR white=COLOR::FromAGBR(255,255,255,255);


          Add least:
          If you should use statics or not is a complete other discussion.






          share|improve this answer




























            4












            4








            4







            The only reason is code cleanliness. You cannot restrict access to a global static variable like



            static int globalValue=5;


            it is (at least) visible in the source file you defined it.
            With a class static, you can give a user of your class hints, how you wish to access it or be accessed. It is only visible within the class scope:



            class myGlobalContainer
            {
            public:
            static int myInt;
            protected:
            static float myFloat;
            private:
            static bool myBool;
            };


            the access of myInt is done by:



            int x=myGlobalContainer::myInt;


            the public modifier gives the user the hint that you see this value as part of the myGlobalContainer and wish him to use it. You do not polute the global namespace like you do with the globalValue.



            The modifier protected and private shows that you do not wish that an "outsider" access those values.



            protected and private static attributes are mostly used to share information between the instances of a class, for e.g. a instance counter:



            class myGlobalContainer
            {
            public:
            myGlobalContainer()
            {
            if(counter==0)
            DoSomeSpecialGlobalInit();
            counter++;
            }
            ~myGlobalContainer()
            {
            counter--;
            if(counter==0)
            DoSomeSpecialGlobalUnInit();
            }
            private:
            static int counter=0;
            };


            public static attributes are often seen with const. They mostly give a user a shortcut. For e.g.:



            COLOR white=COLOR::WHITE;


            instead of:



            COLOR white=COLOR::FromAGBR(255,255,255,255);


            Add least:
            If you should use statics or not is a complete other discussion.






            share|improve this answer















            The only reason is code cleanliness. You cannot restrict access to a global static variable like



            static int globalValue=5;


            it is (at least) visible in the source file you defined it.
            With a class static, you can give a user of your class hints, how you wish to access it or be accessed. It is only visible within the class scope:



            class myGlobalContainer
            {
            public:
            static int myInt;
            protected:
            static float myFloat;
            private:
            static bool myBool;
            };


            the access of myInt is done by:



            int x=myGlobalContainer::myInt;


            the public modifier gives the user the hint that you see this value as part of the myGlobalContainer and wish him to use it. You do not polute the global namespace like you do with the globalValue.



            The modifier protected and private shows that you do not wish that an "outsider" access those values.



            protected and private static attributes are mostly used to share information between the instances of a class, for e.g. a instance counter:



            class myGlobalContainer
            {
            public:
            myGlobalContainer()
            {
            if(counter==0)
            DoSomeSpecialGlobalInit();
            counter++;
            }
            ~myGlobalContainer()
            {
            counter--;
            if(counter==0)
            DoSomeSpecialGlobalUnInit();
            }
            private:
            static int counter=0;
            };


            public static attributes are often seen with const. They mostly give a user a shortcut. For e.g.:



            COLOR white=COLOR::WHITE;


            instead of:



            COLOR white=COLOR::FromAGBR(255,255,255,255);


            Add least:
            If you should use statics or not is a complete other discussion.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 11:51









            Community

            11




            11










            answered May 5 '15 at 7:43









            Martin SchlottMartin Schlott

            3,10911538




            3,10911538

























                1















                Both static class members and static varibles are accessed from member
                functions of any class.




                That is not true:



                class A {
                private:
                static int x;
                };
                int A::x = 5;

                class B {
                static int y;
                public:
                void do_something()
                {
                std::cout << A::x; // Can't access A::x because it's private
                }
                };
                int B::y = 10;


                Although, if we did this:



                static int J = 9;

                class A {
                private:
                static int x;
                };
                int A::x = 5;

                class B {
                static int y;
                public:
                void do_something()
                {
                std::cout << J; // Yes, J is global.
                }
                };
                int B::y = 10;



                • Static member variables can access the private section of it's class opposed to a normal static variable.

                • Static member variables may not be defined inside the class body, unless it's const static or constexpr static.

                • Static member variables may be used as default arguments for the member functions in their class. Opposed to normal static variables, unless they are global.


                Uses: if you want a variable to be alive until the end of your program in both cases but static member variables have access to the private section of that class.






                share|improve this answer


























                • Correct. But public static class members can be accessed by member functions of any other class.

                  – AvadhanaSolutions
                  May 5 '15 at 10:02
















                1















                Both static class members and static varibles are accessed from member
                functions of any class.




                That is not true:



                class A {
                private:
                static int x;
                };
                int A::x = 5;

                class B {
                static int y;
                public:
                void do_something()
                {
                std::cout << A::x; // Can't access A::x because it's private
                }
                };
                int B::y = 10;


                Although, if we did this:



                static int J = 9;

                class A {
                private:
                static int x;
                };
                int A::x = 5;

                class B {
                static int y;
                public:
                void do_something()
                {
                std::cout << J; // Yes, J is global.
                }
                };
                int B::y = 10;



                • Static member variables can access the private section of it's class opposed to a normal static variable.

                • Static member variables may not be defined inside the class body, unless it's const static or constexpr static.

                • Static member variables may be used as default arguments for the member functions in their class. Opposed to normal static variables, unless they are global.


                Uses: if you want a variable to be alive until the end of your program in both cases but static member variables have access to the private section of that class.






                share|improve this answer


























                • Correct. But public static class members can be accessed by member functions of any other class.

                  – AvadhanaSolutions
                  May 5 '15 at 10:02














                1












                1








                1








                Both static class members and static varibles are accessed from member
                functions of any class.




                That is not true:



                class A {
                private:
                static int x;
                };
                int A::x = 5;

                class B {
                static int y;
                public:
                void do_something()
                {
                std::cout << A::x; // Can't access A::x because it's private
                }
                };
                int B::y = 10;


                Although, if we did this:



                static int J = 9;

                class A {
                private:
                static int x;
                };
                int A::x = 5;

                class B {
                static int y;
                public:
                void do_something()
                {
                std::cout << J; // Yes, J is global.
                }
                };
                int B::y = 10;



                • Static member variables can access the private section of it's class opposed to a normal static variable.

                • Static member variables may not be defined inside the class body, unless it's const static or constexpr static.

                • Static member variables may be used as default arguments for the member functions in their class. Opposed to normal static variables, unless they are global.


                Uses: if you want a variable to be alive until the end of your program in both cases but static member variables have access to the private section of that class.






                share|improve this answer
















                Both static class members and static varibles are accessed from member
                functions of any class.




                That is not true:



                class A {
                private:
                static int x;
                };
                int A::x = 5;

                class B {
                static int y;
                public:
                void do_something()
                {
                std::cout << A::x; // Can't access A::x because it's private
                }
                };
                int B::y = 10;


                Although, if we did this:



                static int J = 9;

                class A {
                private:
                static int x;
                };
                int A::x = 5;

                class B {
                static int y;
                public:
                void do_something()
                {
                std::cout << J; // Yes, J is global.
                }
                };
                int B::y = 10;



                • Static member variables can access the private section of it's class opposed to a normal static variable.

                • Static member variables may not be defined inside the class body, unless it's const static or constexpr static.

                • Static member variables may be used as default arguments for the member functions in their class. Opposed to normal static variables, unless they are global.


                Uses: if you want a variable to be alive until the end of your program in both cases but static member variables have access to the private section of that class.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 5 '15 at 8:11

























                answered May 5 '15 at 7:51









                Andreas DMAndreas DM

                6,46552451




                6,46552451













                • Correct. But public static class members can be accessed by member functions of any other class.

                  – AvadhanaSolutions
                  May 5 '15 at 10:02



















                • Correct. But public static class members can be accessed by member functions of any other class.

                  – AvadhanaSolutions
                  May 5 '15 at 10:02

















                Correct. But public static class members can be accessed by member functions of any other class.

                – AvadhanaSolutions
                May 5 '15 at 10:02





                Correct. But public static class members can be accessed by member functions of any other class.

                – AvadhanaSolutions
                May 5 '15 at 10:02











                0














                We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.



                If you declare a static member public then you can access it without member functions as well.. Static has nothing to do with the scope of variable.. It specifies the storage duration only..



                A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.






                share|improve this answer
























                • How is it different than ordinary non-class static variable?

                  – AvadhanaSolutions
                  May 5 '15 at 7:29











                • When we declarea member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. Which is not true with non-static class members.. Different copies of non-static variables are created for each different object

                  – anshabhi
                  May 5 '15 at 8:03











                • static members are used when you have to share some data with all instances of a class.. Suppose, you are designing a video game.. u have 2 heros and 100 enemies.. Then, 100 enemies is declared as static member since both heroes have to use this information..

                  – anshabhi
                  May 5 '15 at 8:09
















                0














                We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.



                If you declare a static member public then you can access it without member functions as well.. Static has nothing to do with the scope of variable.. It specifies the storage duration only..



                A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.






                share|improve this answer
























                • How is it different than ordinary non-class static variable?

                  – AvadhanaSolutions
                  May 5 '15 at 7:29











                • When we declarea member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. Which is not true with non-static class members.. Different copies of non-static variables are created for each different object

                  – anshabhi
                  May 5 '15 at 8:03











                • static members are used when you have to share some data with all instances of a class.. Suppose, you are designing a video game.. u have 2 heros and 100 enemies.. Then, 100 enemies is declared as static member since both heroes have to use this information..

                  – anshabhi
                  May 5 '15 at 8:09














                0












                0








                0







                We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.



                If you declare a static member public then you can access it without member functions as well.. Static has nothing to do with the scope of variable.. It specifies the storage duration only..



                A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.






                share|improve this answer













                We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.



                If you declare a static member public then you can access it without member functions as well.. Static has nothing to do with the scope of variable.. It specifies the storage duration only..



                A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 5 '15 at 7:24









                anshabhianshabhi

                259224




                259224













                • How is it different than ordinary non-class static variable?

                  – AvadhanaSolutions
                  May 5 '15 at 7:29











                • When we declarea member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. Which is not true with non-static class members.. Different copies of non-static variables are created for each different object

                  – anshabhi
                  May 5 '15 at 8:03











                • static members are used when you have to share some data with all instances of a class.. Suppose, you are designing a video game.. u have 2 heros and 100 enemies.. Then, 100 enemies is declared as static member since both heroes have to use this information..

                  – anshabhi
                  May 5 '15 at 8:09



















                • How is it different than ordinary non-class static variable?

                  – AvadhanaSolutions
                  May 5 '15 at 7:29











                • When we declarea member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. Which is not true with non-static class members.. Different copies of non-static variables are created for each different object

                  – anshabhi
                  May 5 '15 at 8:03











                • static members are used when you have to share some data with all instances of a class.. Suppose, you are designing a video game.. u have 2 heros and 100 enemies.. Then, 100 enemies is declared as static member since both heroes have to use this information..

                  – anshabhi
                  May 5 '15 at 8:09

















                How is it different than ordinary non-class static variable?

                – AvadhanaSolutions
                May 5 '15 at 7:29





                How is it different than ordinary non-class static variable?

                – AvadhanaSolutions
                May 5 '15 at 7:29













                When we declarea member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. Which is not true with non-static class members.. Different copies of non-static variables are created for each different object

                – anshabhi
                May 5 '15 at 8:03





                When we declarea member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. Which is not true with non-static class members.. Different copies of non-static variables are created for each different object

                – anshabhi
                May 5 '15 at 8:03













                static members are used when you have to share some data with all instances of a class.. Suppose, you are designing a video game.. u have 2 heros and 100 enemies.. Then, 100 enemies is declared as static member since both heroes have to use this information..

                – anshabhi
                May 5 '15 at 8:09





                static members are used when you have to share some data with all instances of a class.. Suppose, you are designing a video game.. u have 2 heros and 100 enemies.. Then, 100 enemies is declared as static member since both heroes have to use this information..

                – anshabhi
                May 5 '15 at 8:09











                0














                My list of differences:




                1. You can make static class member protected or private.

                2. You can't make static class member global.


                Can't think of anything else.






                share|improve this answer




























                  0














                  My list of differences:




                  1. You can make static class member protected or private.

                  2. You can't make static class member global.


                  Can't think of anything else.






                  share|improve this answer


























                    0












                    0








                    0







                    My list of differences:




                    1. You can make static class member protected or private.

                    2. You can't make static class member global.


                    Can't think of anything else.






                    share|improve this answer













                    My list of differences:




                    1. You can make static class member protected or private.

                    2. You can't make static class member global.


                    Can't think of anything else.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 5 '15 at 7:29









                    AmartelAmartel

                    3,7852819




                    3,7852819























                        0














                        Static class members are used to share data between different instances of a class. The storage for these members is allocated only once and it is only this instance of the static member that will be available for all objects of the class.



                        Static variables inside a function are variables that retain its value through function calls.
                        A classic but simple example of this is a int type static counter in a function where you want to keep track of the number of times this function was called. Since this retains its value through calls, you can increment it inside the function every time it is called.






                        share|improve this answer




























                          0














                          Static class members are used to share data between different instances of a class. The storage for these members is allocated only once and it is only this instance of the static member that will be available for all objects of the class.



                          Static variables inside a function are variables that retain its value through function calls.
                          A classic but simple example of this is a int type static counter in a function where you want to keep track of the number of times this function was called. Since this retains its value through calls, you can increment it inside the function every time it is called.






                          share|improve this answer


























                            0












                            0








                            0







                            Static class members are used to share data between different instances of a class. The storage for these members is allocated only once and it is only this instance of the static member that will be available for all objects of the class.



                            Static variables inside a function are variables that retain its value through function calls.
                            A classic but simple example of this is a int type static counter in a function where you want to keep track of the number of times this function was called. Since this retains its value through calls, you can increment it inside the function every time it is called.






                            share|improve this answer













                            Static class members are used to share data between different instances of a class. The storage for these members is allocated only once and it is only this instance of the static member that will be available for all objects of the class.



                            Static variables inside a function are variables that retain its value through function calls.
                            A classic but simple example of this is a int type static counter in a function where you want to keep track of the number of times this function was called. Since this retains its value through calls, you can increment it inside the function every time it is called.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 5 '15 at 7:44









                            Nirmal ThakurNirmal Thakur

                            11219




                            11219






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30046555%2fstatic-class-member-variable-and-static-variable-in-c%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Homophylophilia

                                Updating UILabel text programmatically using a function

                                Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage