CPB Mailing List
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: static functions
>>>>> "Jody" == Jody Hagins <gamecox@magicnet.net> writes:
Jody> void foo()
Jody> {
Jody> static int i = 1;
Jody> }
Jody> isn't guaranteed to be set to 1 until foo() runs. In fact, for objects,
Jody> ctors definitely won't run until the first time foo() is encountered.
This is an important point and one I have not seen mentioned in this
thread. An important difference between statics in C and in C++ is that
in C++, statics which are defined in a local block have dynamic binding but
global storage. In C, statics must be initialized at compile time.
The following piece of code will compile with a C++ compiler but will
not compile under C:
#include <stdio.h>
void
main()
{
for (int i=0; i<3; ++i) {
static int j = i;
printf("j = %d, i = %d\n", j, i);
}
}
The output is
j = 0, i = 0
j = 0, i = 1
j = 0, i = 2
--
Wojciech Komornicki Dept of Mathematics
wnk@piper.hamline.edu Hamline University
http://www.hamline.edu/~wnk/ St Paul, MN 55104
USA
W Komornicki's Home Page |
Main Index |
Thread Index