Path: utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!sdd.hp.com!hplabs!hpcc05!hpcuhb!hpcllla!hpclisp!hpclscu!shankar From: shankar@hpclscu.HP.COM (Shankar Unni) Newsgroups: comp.lang.c++ Subject: Re: Initialization of static class members? Message-ID: <58170034@hpclscu.HP.COM> Date: 1 Oct 90 23:15:44 GMT References: <114@hewey.dms.cdc.com> Organization: Hewlett-Packard Calif. Language Lab Lines: 42 > class butthead { > static int n = 2; > int c = 1; > }; > > > Which compiler(s) is correct? I find it a big pain to have to explicity > initialize n & c for every constructor I may have. This syntax is an extension to the AT&T language spec, specifically added by g++ only. The "official way" to initialize a static member is not in the declaration, and not in the constructor, but in the actual definition. A static member *must* have an actual externally-visible definition *outside* the class body, in one compilation unit only, as in: // butthead.h class butthead { static int n; }; // butthead.C extern int butthead::n = 0; // definition. As to initializing non-static members, use the member-initialization syntax, as in: class butthead { int n; butthead() : n(0) { } }; Yes, you'll have to specify the initializer in each of the constructors. ----- Shankar Unni E-Mail: Hewlett-Packard California Language Lab. Internet: shankar@hpda.hp.com Phone : (408) 447-5797 UUCP: ...!hplabs!hpda!shankar