1

I am struggling with the static keyword in CPP.

After my experiments I found:

  1. static member is not inside the class. I calculated the size of the class before and after I add a static member, I found they are the same. One explanation that occurred to me is, perhaps the class name become the namespace (scope of this static member)? and that member is outside the class?
  2. Function is not inside the class, the have a implicit argument like the object pointer. however why the size of the class will be one but not 0 if there is no field inside the class but have a bunch of function? but is the pointer also give the function a scope that allow the function to access to the private member or the function its self have the class namespace.
  3. We could use both class name and object to access the static field and function, why we could do that. if the class name is the name space to the static function, why we could use an object to access it? is the object itself have the class namespace?
  4. Then i try to declare and define a static member inside class, and of course it fail, but i want to know why? why we could not initial it inside class?

XD English is not my mother language, sorry for my grammar mistake XD.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • AFAIK static class members don't need a class instance to be used, and the don't pass the `this` pointer for evident reasons. – Iharob Al Asimi Feb 28 '15 at 23:24
  • Can you post sample code to illustrate what you expected vs. what you see? Then we can explain the behavior. – i_am_jorf Feb 28 '15 at 23:25
  • `static` has two meanings in C, and they are the same in C++. C++ adds another meaning which is not in C at all. – user253751 Feb 28 '15 at 23:34
  • I suspect the problem is that you have not actually defined your `static` variable in a source file, just declared it in a header (or source) file. It needs to be defined so that the compiler "knows" where it goes. [In a similar way to declaring an `extern` variable or function in a header file, you then need a definition somewhere in a source file to give the compiler the actual thing] – Mats Petersson Feb 28 '15 at 23:38
  • @immibis C++ adds two additional meanings, one for member functions, and another for member data. And the poster is clearly talking about C++, since he asks about class members. He should remove the C label. – James Kanze Feb 28 '15 at 23:53
  • CPP != C++. CPP is the macro language used to preprocess C and C++ source files; its keywords are things like `#include`. (yes in this case your meaning is clear, but it's the same number of characters, so there's no reason to refer to C++ that way anyway) – Alex Celeste Mar 01 '15 at 00:06

2 Answers2

0

static in c++ has the same semantic than in C:

  • for variables it defines "global" variables which have a single instance, and can only be accessed in a limited scope. This limited scope is:
    • the compilation unit if the variable is defined oustide a function. Note that a static variable defined in a namespace can be accessed in the whole compilation unit with explicit scope resolution.
    • the scope of the function if it was defined in a function,
  • for functions, it limits their visibility to the compilation unit.

In the context of classes, there is a small difference. Static means here that the variable or the function is independent of an object of the class:

  • for member variables this means that there is only one instance of the variable, shared by all the members of the class. Note that it has to be defined somewhere in addition to be declare it in the class if you want to initialize it to a specific value.
  • for member functions, this means that the function can't rely on non static members
Christophe
  • 68,716
  • 7
  • 72
  • 138
0

The keyword static was already seriously overloaded in C, and C++ adds a couple of additional meanings. For any real discussion of what static means, you need to specify the context: at namespace scope, static affects what is called linkage: whether the name being declared refers to the same entity in different sources, or only in this one particular source. Elsewhere, for data, it specifies the lifetime of the entity, not the binding of the name of the entity: there is exactly one instance of a static member variable, which exists independently of any instances of the class which contains it. If a member function is static, this means that it has no this pointer to a specific instance, and can be called without any instance.

With regards to your specific questions:

  1. A static member is not part of any instance of the class, so it doesn't affect the size of the class (which is in fact the size of its instances). The static member is still a member in terms of scope and access controls, but it has static lifetime, and exists outside of any instance of the class.

  2. The sizeof of a concrete object cannot be zero, since even if it contains no data, it cannot have the same address as any other object in memory; if you define an array of such objects, the address of the first element must be different from that of the second element (and sizeof returns the number of bytes you must add to the address of the first element to get the address of the second). The physical size of an object can be 0, at least in certain cases: a base class can have a physical size of 0, for example (provided it is the only base of that type).

  3. I'm not sure what you're actually asking here. When you write something like a.b, b is looked up in the scope of a; if b is a static member, a is only used to determine the scope. When you write A::b, the A:: also specifies thhe scope in which name lookup will occur.

  4. Techically, the declaration of a static data member is not a definition. The reason, or at least the original reason, is that it must be laid out in memory independently of any instances of the class. In the early days, this was required by the then existing compiler technology, and even today, you generally don't want the initialization data in the header file where you define the class.

James Kanze
  • 150,581
  • 18
  • 184
  • 329