static in class
-
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.
-
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.
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
-
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.
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_a1Each object share the
vtbl
and the reference tom_b1
, and allocate memory form_a1
. There will only exist code for the functions and space form_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 form_a1
. /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_a1Each object share the
vtbl
and the reference tom_b1
, and allocate memory form_a1
. There will only exist code for the functions and space form_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 form_a1
. /moliatemoliate 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 thevtable
is implementation dependent. It is correct for VC++ (currently), but might not be for other compilers, or in the future. Peace! -=- James. -
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 thevtable
is implementation dependent. It is correct for VC++ (currently), but might not be for other compilers, or in the future. Peace! -=- James.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
-
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
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 thevtable
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.