Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>static</code> keyword has four separate uses, only two of which are closely related:</p> <ul> <li>static at global and namespace scope (applied to both variables and functions) means internal linkage <ul> <li>this is replaced by <a href="https://stackoverflow.com/questions/154469/unnamed-anonymous-namespaces-vs-static-functions">unnamed namespaces</a> and is unrelated to the rest</li> <li>in particular, others tend to imply some sort of uniqueness, but internal linkage means the <em>opposite</em>: you can have many objects with the same name, as long as each has internal linkage and you only have one per translation unit</li> </ul></li> <li>static data members are "shared" among all instances of the class <ul> <li>it's more like they are independent of any class instance</li> <li>this is sometimes grouped with static methods</li> </ul></li> <li>static methods do not "operate" on a current instance <ul> <li>no <em>this</em> pointer; can call without an instance</li> </ul></li> <li>static local variables (in functions) persist across the scope of each function call</li> </ul> <p>Both static data members and static local variables can become hidden global state, and should be used carefully.</p> <p>Now which two are closely related? It's not the two class members&mdash;the warning about global state gives it away. You can consider static data members as static local variables, where the functions to which they belong are all methods of the class, instead of a single function.</p> <p>I found many <a href="https://stackoverflow.com/search?q=%5Bc%2B%2B%5D+static">related questions</a>, but, surprisingly, no duplicates.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload