Difference between statics in C and statics in C ++ ??

What is the difference between the static keyword in C and C ++?

+46
c ++ c
Jun 03 '09 at 6:06
source share
5 answers

The static performs the same tasks in C and C ++.

  • When used at the file level (outside the function), it sets the visibility of the object to which it is applied. Static elements are not visible outside their compilation unit (for example, for the linker). Their duration coincides with the duration of the program.

    These file-level elements (functions and data) must be static if there is no need to access them from the outside (and almost never require direct access to data, since this violates the central principle of encapsulation).
    <w> If (as your comment on the question points out) this is the only static you are worried about, then no, there is no difference between C and C ++.

  • When used inside a function, it sets the duration of the element. Again, the duration is the same as the program, and the item continues to exist between calls to this function. This does not affect the visibility of this element, since it is visible only inside the function. An example is a random number generator, which should retain its initial value between calls, but does not want this value to be visible to other functions.

  • C ++ has another use, static inside a class. When used there, it becomes the only class variable that is common to all objects of this class. One classic example is to store the number of objects that were created for a given class.

As others have noted, the use of a static file at the file level is deprecated in favor of unnamed namespaces. However, I believe that it will be a cold day in a certain warm place before it is truly removed from the language - there is too much code that is currently using it. And ISO C just got to the gets() deletion, even though we all knew this was a dangerous function.

And even if it is out of date, this will not change its semantics.

+76
Jun 03 '09 at 6:14
source share

Using static in the file domain to restrict access to the current translation system is deprecated in C ++, but still acceptable in C.

Use an unnamed namespace instead

 namespace { int file_scope_x; } 

Variables declared in this way are available only in the file, as if they were declared static.

The main cause of obsolescence is the deletion of one of several overloaded static keyword values.

Initially, this meant that a variable, for example, in a function, would be provided with storage for the program lifetime in the area for such variables and would not be stored on the stack, as usual for local function variables.

Then the keyword was overloaded to apply to file binding. It is not advisable to compose new keywords as needed, as they may violate existing code. Thus, this one was used again with a different value, without causing conflicts, because the variable declared as static cannot be both inside the function and at the top level, and the functions did not have a modifier before. (Storage consolidation is completely lost when accessing functions, because they are not stored anywhere.)

When classes came in C ++ (and in Java and C #), the keyword was used again, but the value is at least closer to the original intent. Variables declared in this way are stored in the global scope, and not on the stack, both for function variables and in the heap for members of an object. Since variables cannot be both at the top level and inside the class definition, additional meaning can be uniquely tied to class variables. They can be referenced only through the class name or inside an object of this class.

+12
Jun 03 '09 at 6:44
source share

It has the same meaning in both languages.

But C ++ adds classes. In the context of a class (and, therefore, structure), it has an additional meaning in order to make class members / class variables more likely members of an object.

 class Plop { static int x; // This is a member of the class not an instance. public: static int getX() // method is a member of the class. { return x; } }; int Plop::x = 5; 
+8
Jun 03 '09 at 6:09
source share

Note that using static to mean β€œfile scope” (as a namespace scope) is only overestimated by the C ++ standard for objects, not functions. In other words:

 // foo.cpp static int x = 0; // deprecated static int f() { return 1; } // not deprecated 

To cite Appendix D of the standard:

The use of the static keyword is deprecated when declaring objects in the namespace scope.

+6
Jun 03 '09 at 8:04
source share

You cannot declare a static variable inside a structure in C ... But it is allowed in Cpp using the scope resolution operator.

Also, in a static Cpp function, you can only access static variables, but in C a static function can have static and non-static variables ... 😊

0
Apr 10 '16 at 16:10
source share



All Articles