Discussion:
static vs global variable
(too old to reply)
Carl Forsman
2009-08-23 21:54:12 UTC
Permalink
static var - call by Class Name e.g. MyClass::Counter

global var - only can call / visiable in the same .cpp file
Doug Harrison [MVP]
2009-08-23 22:12:16 UTC
Permalink
Post by Carl Forsman
static var - call by Class Name e.g. MyClass::Counter
global var - only can call / visiable in the same .cpp file
The relevant concepts are "storage duration" and "linkage". Globals, or
more generally, namespace-scope variables, static member variables, and
local static variables have "static storage duration". Using "static" on a
global variable gives it "internal linkage", which means it can't be seen
outside the translation unit that defines it. This use of "static" to
control linkage has been deprecated in favor of anonymous namespaces, which
unlike static, can also be used to control the visibility of classes that
are intended to be local to a .cpp file. So "static" really should be used
only for member variables and local variables to give them static storage
duration.
--
Doug Harrison
Visual C++ MVP
Loading...