Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!ccu.umanitoba.ca!herald.usask.ca!alberta!brazeau.ucs.ualberta.ca!unixg.ubc.ca!ubc-cs!uw-beaver!zephyr.ens.tek.com!uunet!sdl.mdcbbs.com!alanb From: alanb@sdl.mdcbbs.com Newsgroups: comp.sys.amiga.programmer Subject: Re: Major problem with Comeau C++ (and with old Lattice C++), Part 1 Message-ID: <1991Jun28.182530.1@sdl.mdcbbs.com> Date: 28 Jun 91 17:25:30 GMT References: <195cf3fd.ARN3850@moria.UUCP> Followup-To: adsp.sw,comp.sys.amiga.programmer Organization: Shape Data Ltd. (McDonnell Douglas M&E, Cambridge UK) Lines: 87 Nntp-Posting-Host: shapeg Nntp-Posting-User: alanb In article <195cf3fd.ARN3850@moria.UUCP>, bojsen@moria.UUCP (Per Bojsen) writes: > MAJOR C++ GRIEF ALERT! READ THIS NOW! > > I have found a serious problem with the AT&T cfront Comeau C++ is based on! > This problem is associated with static members of classes, and thus affects > usage of the iostream library (when including iostream.h). > > Consider a class definition: > > class Object > { > public: > Object() { ObjectCount++; } > ~Object() { ObjectCount--; } > > private: > static int ObjectCount; > }; > > All objects of class Object share the ObjectCount member. How can this > behavior be represented in a C program (after all, cfront translates the > C++ source to C)? By defining an external variable of type int to hold > the value of Object::ObjectCount. This is precisely the source of the > problem. Every module that uses (read: defines objects of) class Object > will need to reference this external variable. But it needs to be > *defined* somewhere. [ Explanation of tentative definitions and Unix behaviour summarized - int foo = 0; is a definition, extern int foo; is a declaration int foo; might be either, and some linkers let it be a definition, and then make sure there is only one variable with this name even if it was defined twice. cfront depends on this which blink doesn't do.] (Disclaimer - I haven't even got a C compiler for my Amiga yet. I use cfront on a Sun. No idea if it's the same version (it sticks a comment saying /* <> */ at the top of output.) From Annotated C++ Reference Manual, Ellis and Stroustrup, Section 9.4 "The declaration of a static data member in its class declaration is _not_ a definition. A definition is required elsewhere; see also 18.3" (18.3 says that omitting the definition if you want it all zeros is an anachronism.) The definition looks like Object::ObjectCount = 0; // or -1, or 42, or whatever. because this has an initializer, it is not a _tentative_ definition. Thus the correct solution is i) get cfront to generate declarations which use the `extern' specifier (or compiler/linker options that regard int foo; as a declaration only). and ii) use an explicit definition in exactly one module. > Is there any linker for the Amiga that coalesces multiply defined > objects? > [If you can't get cfront changed, then getting blink (or another linker) to regard all the tentative definitions as the same would solve this problem, and might help port other Unix software. IMHO, it's a workaround and not the solution though. A "common extension" to the standard is not part of the standard] > I guess this is all for now. Any comments are welcome! > .------------------------------------------------------------------------------. > | Greetings from Per Bojsen. | > +------------------------------+-----------------------------------------------+ > | EMail: cbmehq!lenler!bojsen | "Names do have power, after all, that of | > | Or: bojsen@dc.dth.dk | conjuring images of places we have not seen" | > `------------------------------+-----------------------------------------------' --------------------------------------------------------------------------------- Alan Braggins "Any sufficiently simple magic is indistinguishable from advanced technology" Decnet: BBS::SDL::ALANB | Shape Data Limited Internet: alanb@sdl.mdcbbs.com | 46 Regent Street UUCP: {uunet,decwrl,att}!sdl.mdcbbs.com!alanb | Cambridge CB2 1DB Voice: +44 223 316673 Fax: +44 223 316931 | United Kingdom #include