Hello, so I have a quick question regarding the static function and inline function because I don't know if I am getting the correct logic.
Static like this for example -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
static size_t count = 0; // Why does this only get called on the first one?
for (size_t i = 0; i != 10; ++i)
cout << countFuncCalls() << endl;
Could you explain how this works? I don't really see why it only calls static size_t count = 0; on the first call to the function and not all the rest of them. Also how why does count not get destroyed after the function is called? Thank you for the help.
Do not do this. "++i" is superior and it's a good habit to use that form.
line 8: remove the ++ from count
Also do not do that. That would defeat the entire point of the function.
Could you explain how this works? I don't really see why it only calls static size_t count = 0; on the first call to the function and not all the rest of them. Also how why does count not get destroyed after the function is called?
The answer to both of those questions is "because that's what static means".
Static variables have a global lifetime (meaning once created they exist as long as the program remains open), but a local scope (meaning they still cannot be directly accessed outside of whatever function they're defined in).
> I don't really see why it only calls static size_t count = 0; on the first call
The = 0 ; in static size_t count = 0 ; is an initializer; the = here is not assignment.
An object with static storage duration in a local scope is initialized just once; when control reaches the initializer for the first time. Once initialized, its lifetime starts, and it is alive till main() eventually returns (end of program).
int value_with_message( int value, constchar* msg )
std::cout << msg << ": " << value << '\n' ;
return value ;
// global is initialized before the first statement in main is executed
// the lifetime of global is till main() returns
int global = value_with_message( 0, "initialize global with" ) ;
std::cout << '\n' << ++global << ". enter function foo()\n" ;
// local_static is initialized when control reaches line 20 for the first time
// the lifetime of local_static is till main() returns
// the programmatic visibility of local_static is limited to the local scope
staticint local_static = value_with_message( 0, "\tinitialize foo::local_static with" ) ;
// note: local_static is initialized once; the above is initialization (and not assignment)
std::cout << "\tincrement local_static\n" ;
std::cout << "\tlocal_static is now " << local_static << '\n' ;
return local_static ;
std::cout << "\nfirst statement in main()\n" ;
for( int i = 0 ; i != 4 ; ++i ) std::cout << foo() << '\n' ;
Ahh ok thank you guys I think I get it now. So basically you can only use a static variable within the same scope (IE if it is in a function it can only be accessed in a that function) just like any other local variable but instead of the variable being destroyed when the function ends the variable still exists. So I might be useful for keeping track of something over the whole life of the program, or as in my example it would be useful as a counter for how many times that function has been called throughout the whole program.
Is there any other common uses that people use it for? Just wondering because I found i tend to learn a lot better when I figure out common uses for stuff.
> Is there any other common uses that people use it for?
When it may be incorrect to initialize an object more than once; for example if the initialization involves seeding a pseudo random number generator.
When it may be more efficient to reuse a small object in a function that is called many times (instead of constructing an object and then destroying it each time). For example, a random number distribution that could be reused.