Reputation: 67376
It seems to me that a static class variable is identical to an extern variable, because you only declare it in the static int x
/ extern int x
statement, and actually define it elsewhere (usually in a .cpp file)
// .h file
class Foo
{
static int x ;
} ;
// .cpp file
int MyClass::x = 0 ;
// .h file
extern int y;
// .cpp file
int y = 1;
In both cases the variable is declared once somewhere, and defined in a file that will not be included more than once in the compilation (else linker error)
Upvotes: 9
Views: 3329
Reputation: 4143
Yes.
As an additional info, in some programming languages that use non optional namespaces / modules, static class variables can be exchanged with global variables.
In some cases, those other programming variables, doesn't even have static class variables, and you may use global variables, instead.
Some developers prefer use static class variables, enforcing its relation with the class.
Its also a matter of how are you designing your application, even if both features are available.
Upvotes: 1
Reputation: 254771
Yes, both have static storage duration and external linkage; they have essentially the same run-time properties, only differing in (compile-time) visiblity.
Upvotes: 4
Reputation: 154047
More or less. Both have external linkage, and static lifetimes. Both will be initialized on program start-up, and destructed on exit.
Upvotes: 2