If a member is defined as static, then for the class there is only one copy of this member, and not one copy for each instance of the class. Static members can be referenced through an instance (object) of a class. The footnote explains that the expression that identifies the instance is evaluated (and any side effects do occur), even if you don't need to know which instance of the object you get in order to know the value of the static member.
Example:
#include <iostream>
class foo {
public:
static int s;
};
int foo::s = 42;
int index() {
std::cout << "index returns 5\n";
return 5;
}
int main() {
foo arr[10];
std::cout << arr[index()].s << "\n";
}
There is only one object s, and its value 42, but the expression arr[index()]is still evaluated, although its result is not required to determine the value s.
Output:
index returns 5
42
source
share