Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!umich!samsung!zaphod.mps.ohio-state.edu!uwm.edu!rutgers!deimos!ux1.cso.uiuc.edu!ux1.cso.uiuc.edu!m.cs.uiuc.edu!rchen From: rchen@m.cs.uiuc.edu Newsgroups: comp.lang.c++ Subject: another Zortech bug? Message-ID: <4800083@m.cs.uiuc.edu> Date: 9 Feb 90 22:58:00 GMT Lines: 86 Nf-ID: #N:m.cs.uiuc.edu:4800083:000:2848 Nf-From: m.cs.uiuc.edu!rchen Feb 9 16:58:00 1990 I am posting for a friend who has only "read-only" access to this news group. /*-------------------- cut here --------------------------*/ I'm running Zortech 2.0, and possibly the problem is in the compiler, but since the details of how to do this don't seem to be discussed anywhere, I'm not yet ready to point the finger in Walter's direction. The problem is to do with static members in a class, where the static member is 1) an array; and 2) a user-defined class with a single constructor which takes no parameters. So we have a class which is defined along the lines of: class class_with_constructor { int dummy1, dummy2, /* ...*/ ; public: class_with_constructor(void); ~class_with_constructor(void); }; and then another one which uses the first, as in: class class_with_problem { static class_with_constructor array_name[ARRAY_SIZE]; public: class_with_problem(void); ~class_with_problem(void); }; finally, let's specify a global instance of the latter class: class_with_problem global_variable; Now, what I want to happen is to have the constructors for each element in the array "array_name" be called before the call to the constructor to class_with_problem, since the instantiation "global_variable" calls the latter construc- tor, which in turn uses information stored in the array "array_name". This information is intialised in the con- structor for "class_with_constructor". The 2.0 Ref manual merely says, in section 9.4, that "The declaration of a static member in its class declaration is *not* a definition. A definition is required elsewhere". Fine, but where, and how?? Lippman gives an example for a class with a static double member, but it isn't obvious to me how to extrapolate the example to handle arrays; nor is it obvious when or even if a constructor for a user-defined type would be called. I spent about three hours mucking about with various combinations of things yesterday; most of them compiled, but none of them worked. What actually hap- pened in every reasonable case was that class_with_constructor() *was* called ARRAY_SIZE times, but with a "this" defined incorrectly each time; the first call was with this == &global_variable; after that the value of this increased with each call by some amount, probably sizeof(class_with_constructor), although I didn't check that. This behavious is obviously designed to crash a program rapidly...as it did. So, how do I get the constructor called the right number of times and with the first this == &array_name?? Hope this is an easy one for you... All the best Doc Evans SPAN: ORION::DEVANS Snail: Radiophysics, Inc., 5475 Western Ave., Boulder, Colorado 80303 USA Analogue switching network: (303)-447-9524 Non-work-related: Packet (AX.25): NQ0I @ WA8ZIA Packet (TCP/IP): nq0i@nq0i.ampr.org [44.20.0.3]