Timeline for Are file-scope `static` variables in C as bad as `extern` global variables?
Current License: CC BY-SA 3.0
4 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 27, 2015 at 18:22 | comment | added | user186330 | In C at least, static really has one meaning for variables (not for functions). | |
Aug 27, 2015 at 18:19 | comment | added | cmaster - reinstate monica |
I'm talking about members with static storage, which exist exactly once, not for each object. These variable do not need any instance of the class in order to be referenced by the code of the class, so there is no need to pass around a reference/pointer to a singleton. This is precisely what file-scope variables achieve as well. The only difference is, that the static member has class scope while the static "global" variable has file scope. But these two scopes are very similar in extent, which is my point. I agree that the different meanings of static are confusing, though.
|
|
Aug 27, 2015 at 17:32 | comment | added | user186330 | In c++, the syntax kind of encourages you to put a lot of member variables in classes, so having a private static member is not often needed. But in C, if you're not going to fully emulate what you'd do with a C++ class, static or thread_local variables can be used instead of a struct member if you want to avoid passing the struct pointer everywhere. My question is about this. | |
Aug 27, 2015 at 17:23 | history | answered | cmaster - reinstate monica | CC BY-SA 3.0 |