119 questions
84 votes
6 answers
69k views
C++ static initialization order
When I use static variables in C++, I often end up wanting to initialize one variable passing another to its constructor. In other words, I want to create static instances that depend on each other. ...
232 votes
6 answers
235k views
How can I access "static" class variables within methods?
If I have the following code: class Foo(object): bar = 1 def bah(self): print(bar) f = Foo() f.bah() It complains NameError: global name 'bar' is not defined How can I ...
167 votes
2 answers
57k views
How are unnamed namespaces superior to the static keyword? [duplicate]
How are unnamed namespaces superior to the static keyword?
125 votes
12 answers
394k views
What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
I have searched about static variables in C#, but I am still not getting what its use is. Also, if I try to declare the variable inside the method it will not give me the permission to do this. Why? ...
52 votes
5 answers
54k views
C++ static member variable and its initialization
For static member variables in C++ class - the initialization is done outside the class. I wonder why? Any logical reasoning/constraint for this? Or is it purely legacy implementation - which the ...
24 votes
6 answers
115k views
Static vs Instance Variables: Difference?
What is the difference between a static and instance variable. The following sentence is what I cant get: In certain cases, only one copy of a particular variable should be shared by all objects of ...
72 votes
10 answers
69k views
When should I use static methods in a class and what are the benefits?
I have concept of static variables but what are the benefits of static methods in a class. I have worked on some projects but I did not make a method static. Whenever I need to call a method of a ...
59 votes
4 answers
47k views
What makes a static variable initialize only once?
I noticed that if you initialize a static variable in C++ in code, the initialization only runs the first time you run the function. That is cool, but how is that implemented? Does it translate to ...
25 votes
3 answers
7k views
Why does GCC not warn for unreachable code?
Why doesn't GCC (4.6.3) give me any warning for the unreachable code in the below example? #include <stdio.h> int status(void) { static int first_time = 1; if (first_time) { ...
11 votes
4 answers
49k views
Access a global static variable from another file in C
In C language, I want to access a global static variable outside the scope of the file. Let me know the best possible way to do it. One of the methods is to assign an extern global variable the value ...
24 votes
2 answers
14k views
Local variables set to nil? (Objective-C)
I'm reading a book on Objective-C and the author said that if local variables aren't assigned a value they will be set to nil, but static variables will be set to zero. So, I set up int a and didn't ...
15 votes
3 answers
49k views
How to access a variable across two files
I have three files - global.php, test.php, test1.php Global.php $filename; $filename = "test"; test.php $filename = "myfile.jpg"; echo $filename; test1.php echo $filename; I can read this ...
6 votes
3 answers
311 views
Values obtained in case of a recursive function
Can anyone explain to me the reason behind the output of this program to be 0 0 0 0 0? Here we are using a static variable var whose values will not change due to function calls. The values of var ...
6 votes
4 answers
6k views
Calling some functions before main in C
I'd like to do some stuffs before main function. I have multiple source files. In each file, there is some work that needs to be done before main. It was no problem in C++, but problematic with C. In ...
6 votes
6 answers
70k views
How do I declare a static variable inside the Main method?
Can we declare Static Variables inside Main method? Because I am getting an error message: Illegal Start of Expression