Re: "static" objects and functions
jeffjohnson_al...@yahoo.com wrote:
I learned that there are five kinds of static objects, namely
1. global objects
2. object defined in namespace scope
3. object declared static instead classes
4. objects declared static inside functions (i.e. local static
objects)
5. objects declared at file scope.
I have seen the examples of (3) and (4) above, can someone please give
me some examples and their purpose for the other types of static
objects?
For some reason, Google won't let me read your original posting,
so I'll answer here...
What do you mean by "static objects": objects declared with the
keyword static, or objects with static lifetime. The two are
only vaguely related, and you can also use the keyword static to
declare functions. For historical reasons, the keyword static
is overloaded, and depending on context, it affects both object
lifetime and linkage. Roughly speaking:
-- At namespace scope, the keyword static means that the symbol
being defined has internal linkage, rather than external
(which is the default at namespace scope). All objects
declared at namespace scope (with or without the keyword
static) have static lifetime. (Lifetime is irrelevant with
regards to functions.)
-- At class scope, static means that the object or function is
independant of any instance of the class. All symbols
declared at class scope have external linkage; objects
declared with static have static lifetime (as opposed to a
lifetime linked to that of the containing instance of the
class type).
-- At local scope, only objects (not functions) can be declared
static; such objects have static lifetime. (All objects
declared at local scope, unless explicitly declared extern,
have no linkage. Functions declared at local scope always
have external linkage.)
With regards to your exact question, I'm not too sure what
you're asking. A "global object" is normally an object declared
and defined in the global namespace without the static keyword,
i.e. an object in namespace scope and with external linkage. By
definition, it can't be declared static (but it always has
static lifetime), because if it were, it wouldn't be a global
object. And there is no "file scope" in C++; if by "file scope"
you mean outside of any namespace, class or function, then that
is "global scope", or the "global namespace".
Note too that the use of static at namespace scope is
deprecated. It is generally preferred to use an anonymous
namespace. (On the other hand, the names of variables declared
"const" have internal linkage, just as if they were declared
static, unless they are explicitly declared "extern".)
As to when you would use them: the classical singleton idiom is
a good example of static used at class scope:
class Singleton
{
public:
static Singleton& instance() ;
// ...
private:
static Singleton* ourInstance ;
} ;
The function instance() can (and will be) called without an
instance of the object, and the variable ourInstance will have
static lifetime, existing before the first instance is created,
and after the last one is destructed.
At local scope, one might use a static variable to keep track of
recursion:
void*
operator new( size_t n )
{
static bool recursing = false ;
void* result = NULL ;
if ( recursing ) {
result = getMemory( n ) ;
} else {
recursing = true ;
std::cerr << "allocating " << n << " bytes at " ;
result = getMemory( n ) ;
std::cerr << result << std::endl ;
recursing = false ;
}
return result ;
}
This is necessary here, since outputting to std::cerr could very
easily end up calling operator new.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34