int GetNum() { return 5; }
static int num = GetNum();
static int num = GetNum();
static int num2 = num;
int GetNum() { return 5; }
static int num = GetNum();
static int num = GetNum();
static int num2 = num;
Quote:Original post by discman1028
(1) My first is about the static keyword in C++ in the global namespace. When it is used this way, "static" has the C-meaning... the variable is only visible within the scope of the file. It provided partial OO programming in C. I have tested this in VS2005, and it seems to me that I can always #include the header I declared a static global in, and use the static variable. So, how do you actually create a situation where a static global is "hidden" in file-scope?
// test.hstatic int i = 0;void set(int val);void print();// test1.cpp#inclue "test.h"void set(int val) { i = val; }// test2.cpp#inclue "test.h"void print() { cout << i; }// main.cpp#include "test.h"int main(){ i = 10; cout << i; print(); set(20); print(); return 0;}
Quote:Original post by jantaQuote:Original post by discman1028
(1) My first is about the static keyword in C++ in the global namespace. When it is used this way, "static" has the C-meaning... the variable is only visible within the scope of the file. It provided partial OO programming in C. I have tested this in VS2005, and it seems to me that I can always #include the header I declared a static global in, and use the static variable. So, how do you actually create a situation where a static global is "hidden" in file-scope?
Have you tried something like:
...
// test.hint i = 0;// test1.cpp#inclue "test.h"// main.cpp#include "test.h"
// test.hextern int i;// test1.cpp#inclue "test.h"static int i = 0;// main.cpp#include "test.h"
Quote:Original post by discman1028
(1) My first is about the static keyword in C++ in the global namespace. When it is used this way... the variable is only visible within the scope of the file... So, how do you actually create a situation where a static global is "hidden" in file-scope?
namespace{ bool HasFileScope;} // end of unnamed namespace
Quote:Original post by discman1028
I have tested this... I can always #include the header I declared a static global in, and use the static variable.
Quote:Original post by discman1028
(2) The second part of my question: For the Microsoft SDK/architecture I'm working on, declaring constant data structures as "static const" is (according to documentation) supposed to be the fastest way to initialize them... I assume they mean "static" in the "C" context. Any idea why this would make initialization any faster? (However, the static keyword is said to force the compiler to put the variables in the data segment, which makes me suspect that they meant for me to use "static" for any const member variables... i.e. NOT C-style "static".
Quote:Original post by discman1028
Did Microsoft overload the meaning of "static" to get things to fall into the data segment on startup?)
void foo( void ){ for(int i=0; i < 2; ++i) { // j will only be initialized once. Once it exists, it // exists for the life of the program. static int j = 50; j += i; } // j is inaccessable here, due to scope, but still exists. // After first call to foo, j = 51. On second call to foo, // j already exists with value 51, so j's value after second // call would be 52.}
class Bar{public: static int foo; // static member variable foo.}int main( void ){ using std::cout; // the usage of these assumes include of iostream using std::endl; Bar A, B, C; A.foo = 0; // really sets Bar.foo = 0 B.foo += 5; // adds 5 to Bar.foo cout<< C.foo <<endl; // will print 5. // Note that we haven't changed C.foo explicitly, but since // foo is static, only one exists for all instances of Bar class. return 0;}
Quote:Original post by discman1028
(3) Finally: Global variables have an undefined order of initialization, correct? I want to be sure I get the correct behavior.
Quote:Original post by discman1028
I'm still mulling it over how this helped C programmers in terms of working with the object-oriented paradigm.
Quote:Original post by mattnewport
For number 2, if you want to define a value in a header then you have to declare it static (otherwise you'd get multiply defined symbol errors from the linker as already explained).
Quote:Original post by mattnewport
Within a translation unit however, variables are initialized in the order they are defined.
Quote:Original post by twoodbur
Oh... and I'm not sure "static global" even makes sense. If it only exists in file scope, how is it globally accessable?
Quote:Original post by twoodbur
I may be misunderstanding you, and if so, please ignore the following discussion of the "overloads." ...
Quote:Original post by twoodbur
When it's said that the static keyword was used to simulate OOP in C, it is only meant to say that "static" introduced the concept of information hiding (limiting a variable to file scope, in this case) to C.
Quote:Original post by discman1028
Hi all. I have a few questions. ;)
(1) My first is about the static keyword in C++ in the global namespace. When it is used this way, "static" has the C-meaning... the variable is only visible within the scope of the file translation unit. It provided partial OO programming in C. I have tested this in VS2005, and it seems to me that I can always #include the header I declared a static global in, and use the static variable. So, how do you actually create a situation where a static global is "hidden" in file-scope?
Quote:Original post by discman1028
(2) The second part of my question: For the Microsoft SDK/architecture I'm working on, declaring constant data structures as "static const" is (according to documentation) supposed to be the fastest way to initialize them. (i.e. Vector4 newVect = VECTOR4_ZERO; where the latter is a static const.)
Quote:Original post by discman1028
I assume they mean "static" in the "C" context. Any idea why this would make initialization any faster? (However, the static keyword is said to force the compiler to put the variables in the data segment, which makes me suspect that they meant for me to use "static" for any const member variables... i.e. NOT C-style "static". However, if that were not the case... what would making a variable C-style "static" do? Did Microsoft overload the meaning of "static" to get things to fall into the data segment on startup?)
Quote:Original post by discman1028
(3) Finally: Global variables have an undefined order of initialization, correct? I want to be sure I get the correct behavior:
When main() is reached, num is guaranteed to be 5, correct? In what situations is is NOT ok to use num? Is the following guaranteed to be predictable? (I don't think so..)
*** Source Snippet Removed ***
(I guess the static keyword isn't necessary for this last question... static or not, global initialization order is undefined, correct?)
Thanks all.
namespace{ int my_var; // my_var is only defined in this translation unit void my_func() { } // same for my_func}
Quote:Original post by Emmanuel DelogetQuote:Original post by discman1028
Hi all. I have a few questions. ;)
(1) My first is about the static keyword in C++ in the global namespace. When it is used this way, "static" has the C-meaning... the variable is only visible within the scope of the file translation unit. It provided partial OO programming in C. I have tested this in VS2005, and it seems to me that I can always #include the header I declared a static global in, and use the static variable. So, how do you actually create a situation where a static global is "hidden" in file-scope?
Quote:Original post by Emmanuel Deloget
Meh? I corrected your first paragraph, but it seems to me that you are misundersting something: the scope of a static variable or function is the translation unit (to simplify: all the files that are compiled to produce an object file). A static variable which is declared (and defined) in a header is going to generate N different variables with the same name in N different translation unit. To have only one static variable defined, don't put the definition in a header file - put it directly in the cpp file.
Quote:Original post by Emmanuel Deloget
More important: this has nothing to do with OO programming. I don't even understand where you get this idea. Can you elaborate please?
Quote:Original post by Emmanuel DelogetQuote:Original post by discman1028
(2) The second part of my question: For the Microsoft SDK/architecture I'm working on, declaring constant data structures as "static const" is (according to documentation) supposed to be the fastest way to initialize them. (i.e. Vector4 newVect = VECTOR4_ZERO; where the latter is a static const.)
I bet the guy who wrote the documentation has absolutely no clue about what he's talking about. Why would a static const be initialized faster than a const structure? That doesn't make much sense.
Quote:Original post by Emmanuel DelogetQuote:Original post by discman1028
I assume they mean "static" in the "C" context. Any idea why this would make initialization any faster? (However, the static keyword is said to force the compiler to put the variables in the data segment, which makes me suspect that they meant for me to use "static" for any const member variables... i.e. NOT C-style "static". However, if that were not the case... what would making a variable C-style "static" do? Did Microsoft overload the meaning of "static" to get things to fall into the data segment on startup?)
The notion of data segment is unknown to both the C standard and the C++ standard. Conclusion: this kind of information is relying on one implementation that can change in the future.
Quote:Original post by Emmanuel DelogetQuote:Original post by discman1028
(3) Finally: Global variables have an undefined order of initialization, correct? I want to be sure I get the correct behavior:
When main() is reached, num is guaranteed to be 5, correct? In what situations is is NOT ok to use num? Is the following guaranteed to be predictable? (I don't think so..)
*** Source Snippet Removed ***
(I guess the static keyword isn't necessary for this last question... static or not, global initialization order is undefined, correct?)
Thanks all.
This code just won the How to Enhance Unreadability Award, 2007 Edition [grin].
More seriously,
1) you should avoid global variables
2) you should avoid global variables
3) when you can't avoid global variables, try to avoid them anyway
Quote:Original post by Emmanuel Deloget
Anyway, your first assumption is correct: num is more or less guaranteed to be 5 when you enter main() (any other behavior would break a lot of programs anyway). However, you'd better not using num as an initialization value outside main() - and especially not in other translation units. The second code snippet is a bit more problematic: num may not have been initialized before num2 is initialized. So you can get two undefined values for the price of one (although num might be initialized later).
Quote:Original post by Emmanuel Deloget
In C++, the static keyword is never necessary (unless it's used to specify a static class member, of course). It is even far better to replace it by an anonymous namespace, which has the same effect on your code:namespace{ int my_var; // my_var is only defined in this translation unit void my_func() { } // same for my_func}
Quote:Original post by Emmanuel Deloget
Regarding the questions in your latest post: I suggest you to take a rest and to explain us what you are really doing, because the solution you want to use is obviously not the right solution, whatever your problem is.
Quote:
To force the compiler to put the Vector data into the data segment, you need to mark your Vector constants as static constants.
By adding the static keyword, the compiler knows to place the data into the data segment so that it can be loaded with 3 instructions, instead of regenerating the constant each time that the code is executed. By marking it as a constant, you avoid accidentally and permanently changing the values.