In the following example -
#include <iostream>
int someMethod(){
static int a = 20;
static const int result = a + 1;
++a;
std::cout << " [" << a << "] ";
return result;
}
int main(){
std::cout << someMethod() << "\n";
std::cout << someMethod() << "\n";
std::cout << someMethod() << "\n";
}
The output comes as -
[21] 21
[22] 21
[23] 21
What is the reason that is preventing result
value to be modified on subsequent calls made to the same function? I've printed the output of variable a
as well, which is certainly being incremented and since it is static as well there must not be multiple copies existing for the same method.
IDEONE - COMPILER OUTPUT
How is static content cached? The usual web caching process is for a cache to save a copy of the static file – e.g., an image, – when the content is served, so that it's closer to the user and delivered more quickly the next time. Browsers and content delivery networks (CDNs) can cache static content for a set time period ...
While many forms of caching are available, static caching is a method for converting the page generated by a user’s request into an HTML document to serve any subsequent requests to that same page.
A static cache is just that: static. Any changes made to the site will not be reflected on pages cached in this manner. Information such as “items in cart” or other pieces of data stored in your customers’ session files will not function on these pages.
Using cURL or Wget, make requests to the target pages and store them in the directory under their respective URI names (index, products, etc) Add rewrites to the top of your .htaccess file to redirect requests to the static cache files, such as the following:
Since result
is static, it will be initialized only once during runtime.Therefore the following line is executed only once no matter how many times you call someMethod()
static const int result = a + 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With