c++ - "Private" variable in namespace scope in cpp file -
in source file (not header file), have habit of placing state implementation can work with:
//.cpp namespace foo{ sometype bar; //... functions , objects might use bar ... }
because bar
declared in cpp file, not possible other code files use it, correct?
note, however, have not placed static
before it. in case, though, should make no difference?
without static
, difference variable accessed (and set) code file if in other file -- either header or cpp -- had extern
declaration same variable name? (and of course, declaration within namespace of same name)
since not have such extern
declaration anywhere else in code base, gather makes no difference whether mark static
or not -- wanted make sure thinking correct.
you correct, static
on variables in namespace scope controls visibility of variable other translation units (internal vs. external linkage). apart that, behavior of variable going remain same. example, storage class remain static. difference without static
declaration
namespace foo{ extern sometype bar; ... }
from other translation unit going "connect" bar
variable, while static
extern
fail @ linking time.
Comments
Post a Comment