Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!lll-winken!unixhub!ditka!comeau From: comeau@ditka.Chicago.COM (Greg Comeau) Newsgroups: comp.lang.c++ Subject: Re: Static member definition vs. declaration? Message-ID: <37226@ditka.Chicago.COM> Date: 21 Apr 91 17:10:55 GMT References: <1848@dinl.mmc.UUCP> <186@devnull.mpd.tandem.com> Sender: news@ditka.chicago.com (Pulitzer at ditka) Reply-To: comeau@csanta.attmail.com (Greg Comeau) Organization: Comeau Computing Lines: 39 In article <186@devnull.mpd.tandem.com> kelley@mpd.tandem.com (Michael Kelley) writes: >In article <1848@dinl.mmc.UUCP>, noren@dinl.uucp (Charles Noren) writes: >> class X {static int myVar;}; >> Do you need to make a definition elsewhere, >> int X::myVar; >> I claim you do (and of course you want to initialize it You *do*. A static member only serves as a declaration, and not a definition hence a definition still needs to be provided. >Static members of fundamental types, or of a class with no constructor, >needn't be defined by the programmer. Cfront 2.0 simply lays down a Check out the ARM, p179: "The declaration of a static data member in its class declaration is *not* a definition. A definition is required elsewhere". This characteristic is definitely in the 2.1 language spec and I believe was also in 2.0's (although I'm not sure anymore). >definition for these critters, and lets the linker resolve them all >to the same address. It can't be done for objects with constructors, It's not quite that it lets the linker but that that's how the UNIX linker, like most linkers, unfortunately work (in the so-called relaxed definition mode of ANSI C). In any event, implementation are currently in a state of transition on this issue. Nevertheless, it is an anacronism in all cases. >sense. I imagine that in future (2.2+?) releases, you will get link >errors on all static members not explicitly defined, and not just >those with constructors like you do now. Again, 2.1 already has this. It's up to the "compiler system" to enforce it. - Greg -- Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418 Producers of Comeau C++ Here:attmail.com!csanta!comeau / BIX:comeau / CIS:72331,3421 Voice:718-945-0009 / Fax:718-441-2310