diff options
-rw-r--r-- | 6p6.cpp | 14 | ||||
-rw-r--r-- | 6p7.cpp | 30 |
2 files changed, 44 insertions, 0 deletions
@@ -0,0 +1,14 @@ + +/* + * + * 6.6 + * + * + */ + +int main () { + + // When the function ends the local variable is destroyed + // When the function ends the static variable retains its value and is still there when the function is called again. + return 0; +} @@ -0,0 +1,30 @@ +#include <iostream> + +/* + * + * 6.7 + * + * + */ + +int ThisFunctionHasAStaticVariable() { + + static int i = -1; + ++i; + return i; +} + +int main () { + + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + std::cout << ThisFunctionHasAStaticVariable() << std::endl; + return 0; +} |