Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. keyword 'static'

keyword 'static'

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N namaskaaram

    Is there a difference naming a static member variable of a class private?? how is a static public member variable different from a static public member variable??

    E Offline
    E Offline
    Eytukan
    wrote on last edited by
    #3

    I could guess your question. Static and private has totally different meanings. when you declare a variable private but not static, it means that it can be used within the same class. It cannot be accessed by other members(may be another class). and more importantly, every time an Object is instantiated, a new instance of the variable is created. But when you declare a varible private and also static. It's all the same but the last point, which when new objects are created, it's not instantiated anew, rather all objects share the variable. and when you want to access it, you cannot use the . or the -> operator, you should use the :: operator with the class name preceeding it. now you get it? note, you cannot put "static" in the private,protected,public list. it can coexist with all the three ;)


    --[V]-- [My Current Status]

    N 1 Reply Last reply
    0
    • N namaskaaram

      Is there a difference naming a static member variable of a class private?? how is a static public member variable different from a static public member variable??

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #4

      don't confuse !!! static don't alter the visibility accessors (public, protected, private). it only tells the compiler that the member declared as static will exist once and will be shared between all the instances of the class... do you get the difference then ?


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

      N 1 Reply Last reply
      0
      • N namaskaaram

        Is there a difference naming a static member variable of a class private?? how is a static public member variable different from a static public member variable??

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #5

        The static-ness and access of a member variable aren't mutually exclusive. All static means is that there is only one copy of that member for all instances of the class. public/protected/private access all work the same as with non-static members.

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

        N 1 Reply Last reply
        0
        • E Eytukan

          I could guess your question. Static and private has totally different meanings. when you declare a variable private but not static, it means that it can be used within the same class. It cannot be accessed by other members(may be another class). and more importantly, every time an Object is instantiated, a new instance of the variable is created. But when you declare a varible private and also static. It's all the same but the last point, which when new objects are created, it's not instantiated anew, rather all objects share the variable. and when you want to access it, you cannot use the . or the -> operator, you should use the :: operator with the class name preceeding it. now you get it? note, you cannot put "static" in the private,protected,public list. it can coexist with all the three ;)


          --[V]-- [My Current Status]

          N Offline
          N Offline
          namaskaaram
          wrote on last edited by
          #6

          I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????

          E 1 Reply Last reply
          0
          • T toxcct

            don't confuse !!! static don't alter the visibility accessors (public, protected, private). it only tells the compiler that the member declared as static will exist once and will be shared between all the instances of the class... do you get the difference then ?


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            N Offline
            N Offline
            namaskaaram
            wrote on last edited by
            #7

            I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????

            1 Reply Last reply
            0
            • M Michael Dunn

              The static-ness and access of a member variable aren't mutually exclusive. All static means is that there is only one copy of that member for all instances of the class. public/protected/private access all work the same as with non-static members.

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

              N Offline
              N Offline
              namaskaaram
              wrote on last edited by
              #8

              I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????

              D M 2 Replies Last reply
              0
              • N namaskaaram

                I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????

                E Offline
                E Offline
                Eytukan
                wrote on last edited by
                #9

                If it is private , it means it. No matter it is static or not. You cannot access even with a :: operator.


                --[V]-- [My Current Status]

                N 1 Reply Last reply
                0
                • E Eytukan

                  If it is private , it means it. No matter it is static or not. You cannot access even with a :: operator.


                  --[V]-- [My Current Status]

                  N Offline
                  N Offline
                  namaskaaram
                  wrote on last edited by
                  #10

                  thank u! :)

                  1 Reply Last reply
                  0
                  • N namaskaaram

                    Is there a difference naming a static member variable of a class private?? how is a static public member variable different from a static public member variable??

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #11

                    namaskaaram wrote:

                    how is a static public member variable different from a static public member variable??

                    From my vantage point, they are identical.


                    "The largest fire starts but with the smallest spark." - David Crow

                    "Judge not by the eye but by the heart." - Native American Proverb

                    1 Reply Last reply
                    0
                    • N namaskaaram

                      I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #12

                      namaskaaram wrote:

                      So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????

                      Have you tried it to see?


                      "The largest fire starts but with the smallest spark." - David Crow

                      "Judge not by the eye but by the heart." - Native American Proverb

                      1 Reply Last reply
                      0
                      • N namaskaaram

                        I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????

                        M Offline
                        M Offline
                        Michael Dunn
                        wrote on last edited by
                        #13

                        class test { private: static int foo; };
                        int test::foo = 1;

                        main()
                        {
                        int x = test::foo;
                        }

                        error C2248: 'foo' : cannot access private member declared in class 'test'

                        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups