Use of static variables and functions in global scope?

In this case, keyword static means the function or variable can only be used by code in the same cpp file. The associated symbol will not be exported and won't be usable by other modules.

Up vote 3 down vote favorite 1 share g+ share fb share tw.

Is there a use for flagging a variable as static, when it lies in the global scope of a . Cpp file, not in a function? Can you use the static keyword for functions as well?

If yes, what is their use? C++ static global-variables global static-variables link|improve this question edited Jan 18 '11 at 15:03Nawaz70.5k979206 asked Jan 18 '11 at 14:29org 100h182.

1 Sounds like homework – Antonio Pérez Jan 18 '11 at 14:35 3 Sounds like someone calling anything homework – org 100h Jan 18 '11 at 14:49.

In this case, keyword static means the function or variable can only be used by code in the same cpp file. The associated symbol will not be exported and won't be usable by other modules. This is good practice to avoid name clashing in big software when you know your global functions or variables are not needed in other modules.

In C++ one should use unnamed namespaces. – Matthieu M. Jan 18 '11 at 16:25.

Yes, if you want to declare file-scope variable, then static keyword is necessary. Static variables declared in one translation unit cannot be referred to from another translation unit. By the way, use of static keyword is deprecated in C++03.

The section $7.3.1. 1/2 from the C++ Standard (2003) reads, The use of the static keyword is deprecated when declaring objects in a namespace scope; the unnamed-namespace provides a superior alternative. C++ prefers unnamed namespace over static keyword.

See this topic: Superiority of unnamed namespace over static?

1 And the latest C++0x draft undeprecates it. – Fred Nurk Jan 18 '11 at 14:53 @Fred : that's interesting! – Nawaz Jan 18 '11 at 15:01 @Fred: amusing, it changed between n3092 and n3225, do you know what motivated this change?

– Matthieu M. Jan 18 '11 at 16:27 @Matthieu: that's even more interesting. Can you please tell us what motivated this change?

Or at least refer us to a link? – Nawaz Jan 18 '11 at 16:29 I could not find anything really relevant, I've asked the question ( stackoverflow.com/questions/4726570/… ), let's hope someone on SO knows something about it. – Matthieu M.

Jan 18 '11 at 16:41.

Taking as an example - // At global scope int globalVar; // Equivalent to static int globalVar; // They share the same scope // Static variables are guaranteed to be initialized to zero even though // you don't explicitly initialize them. // At function/local scope void foo() { static int staticVar ; // staticVar retains it's value during various function // function calls to foo(); } They both cease to exist only when the program terminates/exits.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions