Why don't we have different copies of static variables for the different objects?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
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.Because class static members are stored separately in BSS section, so every instance of a class has the same value.
Because the section $9.4.2/1 from the C++ Standard (2003) says,
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?
Because that's what
static
means in that context.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).
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.