Article View: comp.std.c++
Article #1612Re: Calling ctors. from ctors.
From: Ralf Boecker
Date: Thu, 03 Aug 1995 14:25
Date: Thu, 03 Aug 1995 14:25
45 lines
2383 bytes
2383 bytes
page@cat.rpi.edu (Lance Page) wrote:
>I often wish I could build A::A(some args...) from A::A(different args...).
>I propose (for discussion) allowing a constructor
>Example: consider the class...
>class A {
> int *ptr;
> int x;
> int y;
> public:
> A() : ptr(0) {}
> A(int xval, int yval) : ptr(0), x(xval), y(yval) {}
> A(const A &from) : ptr(0), x(from.x), y(from.y) {}
>};
>
>// I would like be able to write the last constructor in terms of the
>// previous one, i.e.:
> A(const A &from) : A(from.x, from.y) {}
>
>Motivation:
> ** Source code advantages:
> If there are key requirements for constructors of this class,
> (e.g. initializing some special pointers or references), it is
> error-prone and bulky to have to replicate these initializations in all
> the constructors.
..
I agree with You.
But you should chose a better example, where the calling of
base class / member c-tors performs aditional operations like
new ore some calculations: You can easyly factor out {}-initialization-code
in some init() funktion (but resulting in one additional function call
execution time penalty), but code for calling c-tors is normaly replicated.
You can only workaround this by introducing an additional derived 'padding'
class whose c-tor will perform necessary calculations to be reused by the
final class c-tors. This is tedious.
I could live without it, but I can see no reason, why this handsome
feature should not be added to the standard.
If anybody has one, I would like to know it.
--
*** Dipl.-Ing. Ralf Boecker Universitaet GH Kassel ***
*** eMail: boecker@hrz.uni-kassel.de FB 16 - Elektrotechnik ***
*** Tel: (+49)561/804-6404 Wilhelmshoeher Allee 73 ***
*** Fax: (+49)561/804-6360 D-34121 Kassel (Germany) ***
Technical Details
Message-ID:
<3vqm8o$g7f@hrz-ws11.hrz.uni-kassel.de>Path:
arc1-ca.pugleaf.net!hrz-ws11.hrz.uni-kassel.de!newsReferences:
<3v5lo9$pql@usenet.rpi.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!hrz-ws11.hrz.uni-kassel.de!news From: Ralf Boecker <boecker@hrz.uni-kassel.de> Newsgroups: comp.std.c++ Subject: Re: Calling ctors. from ctors. Date: 3 Aug 1995 14:25:28 GMT Organization: GHK-FB16 Message-ID: <3vqm8o$g7f@hrz-ws11.hrz.uni-kassel.de> References: <3v5lo9$pql@usenet.rpi.edu> NNTP-Posting-Host: uet-pc8.uet.e-technik.uni-kassel.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 1.1N (Windows; I; 16bit) To: page@cat.rpi.edu