lovecreatesbeauty wrote:
But a static data member (of the same type as the class) will take up
space always.
Yes, but not in each instance of the class. Static data members exist
in their one-per-class uniqueness.
When define a static data member of the same type as the
class, the class itself is incomplete, how to calculate the size of
the space, and why the members of this static can be referenced?
The size of the class is the sum of the sizes its base class sub-objects,
and its non-static data, with some additions like padding and supplemental
elements (all implementation-defined) like vtbl, virtual base class ptr.
Static data members do NOT participate in the calculation of the final
size of the class instance.
When you declare a static data member in a class, it's a declaration, no
memory is allocated untill the _definition_ is encountered, and it is
_always_ outside (i.e. after) the class definition, and at that time the
class is complete. Static data members can be referenced just like
member functions, for example, for which only a declaration is required
(a definition will be required to complete the program).
You may think of static data members like of global data whose scope is
limited to their class and who also has access rights defined for them.
Aside from scope and access (that actually only have meaning while the
code is being compiled, anyway), the static data members are nothing but
global data.
V