Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static const cached result

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

like image 360
Abhinav Gauniyal Avatar asked Dec 22 '16 04:12

Abhinav Gauniyal


People also ask

How is static content cached?

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 ...

What is static caching and how does it work?

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.

What is a static cache in Salesforce?

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.

How do I redirect a page to a static cache file?

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:


1 Answers

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; 
like image 160
Gaurav Sehgal Avatar answered Oct 20 '22 08:10

Gaurav Sehgal