home *** CD-ROM | disk | FTP | other *** search
- //: C10:StaticMemberFunctions.cpp
- // From Thinking in C++, 2nd Edition
- // Available at http://www.BruceEckel.com
- // (c) Bruce Eckel 1999
- // Copyright notice in Copyright.txt
-
- class X {
- int i;
- static int j;
- public:
- X(int ii = 0) : i(ii) {
- // Non-static member function can access
- // static member function or data:
- j = i;
- }
- int val() const { return i; }
- static int incr() {
- //! i++; // Error: static member function
- // cannot access non-static member data
- return ++j;
- }
- static int f() {
- //! val(); // Error: static member function
- // cannot access non-static member function
- return incr(); // OK -- calls static
- }
- };
-
- int X::j = 0;
-
- int main() {
- X x;
- X* xp = &x;
- x.f();
- xp->f();
- X::f(); // Only works with static members
- } ///:~
-