Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: notesfiles Path: utzoo!watmath!clyde!cbosgd!hplabs!hp-pcd!orstcs!nathan From: nathan@orstcs.UUCP (nathan) Newsgroups: net.lang.c++ Subject: Initializing static members Message-ID: <34200012@orstcs.UUCP> Date: Fri, 28-Mar-86 21:00:00 EST Article-I.D.: orstcs.34200012 Posted: Fri Mar 28 21:00:00 1986 Date-Received: Wed, 2-Apr-86 01:39:26 EST Organization: Oregon State University - Corvallis, OR Lines: 43 Nf-ID: #N:orstcs:34200012:000:1457 Nf-From: orstcs!nathan Mar 28 18:00:00 1986 Initialization of static members: I recently discussed C++ with a fine fellow at Mentor Graphics (who may wish to remain anonymous; I neglected to ask), and the question came up of how to initialize a static class member. With the restriction that no initializer may be provided for the field (apparently a syntactic hurdle), and that (!) no constructor may be provided for the class containing it (!), the problem begins to look interesting. Question: can a constructor be provided for a derivation of a class containing a static? If so, we could do this: // in bleah.h class bleah { static int here; public: int Here() { return 100; } } ... // in bleah.c class bleah_hack : bleah { bleah_hack() { here = Here(); } } b; Some notes about the example are in order: The member function Here() is pretending to be a constant field, but it's not even a constant *expression* for initializer purposes. One hopes the optimizer can make it as efficient as a true constant. By the way, 'b' above defaults to static, not extern; see r8.1. If constructors for the derived class are illegal, a friend class may be needed, kluge-ier yet. This whole problem, of consts and statics in class declarations, seems pretty fundamental -- as if the restriction on constructors, if circumvented this way and not rejected by the compiler, might result in bad code. Who understands this issue? Nathan C. Myers orstcs!nathan nathan@oregon-state