static Members Can Be Used in Ways Ordinary Members Can’t

As we’ve seen, static members exist independently of any other object. As a result, they can be used in ways that would be illegal for nonstatic data members. As one example, a static data member can have incomplete type (§ 7.3.3, p. 278). In particular, a static data member can have the same type as the class type of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class:

class Bar {public:    // ...private:    static Bar mem1; // ok: static member can have incomplete type    Bar *mem2;       // ok: pointer member can have incomplete type    Bar mem3;        // error: data members must have complete type};

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.