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. static in class

static in class

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 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 Offline
    N Offline
    NicholasCougar
    wrote on last edited by
    #1

    Hi, Plz look into this class, what will happen with static member variable? class A { private: char m_a1; static double m_b1; }; Tell me in detail, plz. Thank you in advance. Best regard. I confess that I am a stubborn guy, but why not put things thoroughly, logically and systematically clean. One concrete prolem is worth a thousand unapplied abstractions.

    C M 2 Replies Last reply
    0
    • N NicholasCougar

      Hi, Plz look into this class, what will happen with static member variable? class A { private: char m_a1; static double m_b1; }; Tell me in detail, plz. Thank you in advance. Best regard. I confess that I am a stubborn guy, but why not put things thoroughly, logically and systematically clean. One concrete prolem is worth a thousand unapplied abstractions.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      The variable m_b1 will exist once across all instances of this class. It could be set or accessed using A:m_b1, except that you've made it private. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002

      1 Reply Last reply
      0
      • N NicholasCougar

        Hi, Plz look into this class, what will happen with static member variable? class A { private: char m_a1; static double m_b1; }; Tell me in detail, plz. Thank you in advance. Best regard. I confess that I am a stubborn guy, but why not put things thoroughly, logically and systematically clean. One concrete prolem is worth a thousand unapplied abstractions.

        M Offline
        M Offline
        moliate
        wrote on last edited by
        #3

        This might not be fully accurate, but I think the memory structure of an object created with the class will look something like this:

        vtbl -> (functions)
        m_b1 -> (one single instance of double)
        m_a1

        Each object share the vtbl and the reference to m_b1, and allocate memory for m_a1. There will only exist code for the functions and space for m_b1 at one place, no matter how many objects you create from the class. On the other hand each object will be able to access their own memory for m_a1. /moliate

        J 1 Reply Last reply
        0
        • M moliate

          This might not be fully accurate, but I think the memory structure of an object created with the class will look something like this:

          vtbl -> (functions)
          m_b1 -> (one single instance of double)
          m_a1

          Each object share the vtbl and the reference to m_b1, and allocate memory for m_a1. There will only exist code for the functions and space for m_b1 at one place, no matter how many objects you create from the class. On the other hand each object will be able to access their own memory for m_a1. /moliate

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #4

          moliate wrote: This might not be fully accurate [...]    Actually, it is pretty good, except for two things:       1: A class will not have a vtable in it's layout unless it has virtual methods (or inherits them from a base class).       2: The layout (placement) of the vtable is implementation dependent.  It is correct for VC++ (currently), but might not be for other compilers, or in the future.    Peace! -=- James.

          M 1 Reply Last reply
          0
          • J James R Twine

            moliate wrote: This might not be fully accurate [...]    Actually, it is pretty good, except for two things:       1: A class will not have a vtable in it's layout unless it has virtual methods (or inherits them from a base class).       2: The layout (placement) of the vtable is implementation dependent.  It is correct for VC++ (currently), but might not be for other compilers, or in the future.    Peace! -=- James.

            M Offline
            M Offline
            moliate
            wrote on last edited by
            #5

            Thanks for your comments. I know that the placement ot the vtbl is implementation dependant (thanks to painful experiences with Mac). I am still a bit unclear about non-virtual member functions, however. I assume that they are not replicated in every object instance? Is there a separate pointer like the vtbl for them? Could you help me to become a better C++ programmer? :) Cheers /moliate

            J 1 Reply Last reply
            0
            • M moliate

              Thanks for your comments. I know that the placement ot the vtbl is implementation dependant (thanks to painful experiences with Mac). I am still a bit unclear about non-virtual member functions, however. I assume that they are not replicated in every object instance? Is there a separate pointer like the vtbl for them? Could you help me to become a better C++ programmer? :) Cheers /moliate

              J Offline
              J Offline
              James R Twine
              wrote on last edited by
              #6

              moliate wrote: I am still a bit unclear about non-virtual member functions, however. I assume that they are not replicated in every object instance? Is there a separate pointer like the vtbl for them?    IIRC, neither virtual or "normal" member functions are replicated for every instance of a object.  The old idea of "one copy of code, multiple copies of data" comes to mind.    Yes, if the object contains virtual methods, then each instance of the object gets a copy of it's vtable.  This is needed for polymorphism.  But it is just the vtable that is in the memory layout of the object, not the code for the functions themselves.  That code still exists in one place, and the vtable is used to get to them. moliate wrote: Could you help me to become a better C++ programmer?    That is what this site is (supposed to be) for!     :P    (Note, I used "object" above instead of "class" because both classes and structs can contain member functions...)    Peace! -=- James.

              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