You can directly refer to a static member in the same
scope of its class, or in the scope of a class derived from the static
member's class. The following example demonstrates the latter case
(directly referring to a static member in the scope of a class derived
from the static member's class):
#include <iostream>
using namespace std;
int g() {
cout << "In function g()" << endl;
return 0;
}
class X {
public:
static int g() {
cout << "In static member function X::g()" << endl;
return 1;
}
};
class Y: public X {
public:
static int i;
};
int Y::i = g();
int main() { }
The following is the output of the above
code:
In static member function X::g()
The initialization
int
Y::i = g() calls
X::g(), not the function
g() declared
in the global namespace.