Xref: utzoo comp.lang.c++:13752 comp.std.c++:943 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cis.ohio-state.edu!ucbvax!bloom-beacon!eru!hagbard!sunic!sics.se!fuug!fuug.fi!pena From: pena@brainware.fi (Olli-Matti Penttinen) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Re: Constructor Limitation: [Was: Re: Constructor question] Message-ID: Date: 29 May 91 18:51:38 GMT Sender: usenet@fuug.fi (USENET News System) Organization: Brainware Oy, Espoo, Finland. Lines: 67 In-Reply-To: marc@mit.edu's message of 27 May 91 18: 09:43 GMT Nntp-Posting-Host: brainw.brainware.fi In article marc@mit.edu (Marc Horowitz) writes: [ lotsa stuff zapped ] I'll start by saying that this problem should definitely be looked at. It apparently happens at least somewhat often, since it happened to me last night. I had an even more difficult situation. My classes look like this: class A { public: A(int a, int b, int c, int d); // ... }; class B : public A { public: B(char *s) :A(/* ??? */) { /* too late */ } // ... }; void f(const char *s, int *a, int *b, int *c, int *d); f takes a string, and parses the integers out of it. It's somewhat expensive. Now, how should I go about doing this? Every solution I've been able to come up with is a morally abhorrent kludge involving some large number of static variables. I really would like to be able to have a block where I could define a few local temporaries, call f, and pass those temps into the base constructor. I could add another constructor to A, but A shouldn't need to depend on f, since f is really a part of B. Also note that subclassing won't help, since the arguments to A aren't functions of a common temporary, as f(x) and f(x)+1 are in the original example. Do you ever use f for anything but to initialize or change the value of some A. If not, then f would really belong in A. In anycase, why not add A::A(const char *); that uses f to compute the 4 ints. Besides, Bjarne's proposal to use a protected init-method works well, too, iff A has a default constructor. Admittedly, things might get a little dirty if an A cannot have a meaningful state constructed out of nothing. In that case, one could (accidentally) use the value of an A before the object is fully constructed, so the problem would still be there. All in all, I don't see a reason to extend the language just because a new feature would occasionally save a few keystrokes. [ more text deleted ] Marc -- Marc Horowitz 617-253-7788 ==pena -- Olli-Matti Penttinen | "When in doubt, use brute force." Brainware Oy | --Ken Thompson P.O.Box 330 +---------------------------------- 02151 ESPOO, Finland Tel. +358 0 4354 2565 Fax. +358 0 461 617