Why don't we have different copies of static variables for the different objects?
-
6Oh seriously, come on. 2 downvotes? Just because they don't understand a crucial concept of what `static` means (yet)? There's likely more people who make that mistake, especially in their early programming days. And why does that make this question less clear or useful than, say, »Why is Random not so random?« (which may equally be categorized in »common sense«)? I recall that some time ago the FAQ said that even beginner questions are welcome here. – Joey Jan 30 '11 at 09:26
-
1@Joey: I agree, no question should warrant a downvote merely because it shows ignorance. We all need to begin somewhere and this question, while succinct, is well-written and goes straight to the point. – Matthieu M. Jan 30 '11 at 14:23
7 Answers
Because they would be instance members then.
The primary characteristic of static members is that they're shared by all the instances of the class.

- 258,201
- 41
- 486
- 479
Because the section $9.4.2/1 from the C++ Standard (2003) says,
A static data member is not part of the subobjects of a class. There is only one copy of a static data member shared by all the objects of the class.
Since the Standard alone decides what C++ is, what not, so it's how C++ has been designed!
Static members are more like global objects. The same copy belong to all objects!
See this post for detail answer : Do static members of a class occupy memory if no object of that class is created?
A static member is not associated with a specific instance.
If you want different values of the member for each instance you should use instance members (remove the static keyword).

- 811,555
- 193
- 1,581
- 1,452
It's by definition- a static object is one that's shared by all instances of the class. Regular members don't have this property.

- 362,284
- 104
- 897
- 1,065
That is the definition of static
- one copy of the data exists. It is separately stored, most likely along with all the other static data of the library or application.

- 5,057
- 3
- 25
- 30
Because class static members are stored separately in BSS section, so every instance of a class has the same value.

- 364
- 2
- 3
-
1*Maybe.* But once again, that's an implementation detail, not the key to genuinely understanding what's happening (or what's guaranteed by the spec). – Cody Gray - on strike Jan 30 '11 at 09:36