Article View: comp.std.c++
Article #1624Re: Calling ctors. from ctors.
From: Ramesh Seshadri
Date: Sun, 06 Aug 1995 19:35
Date: Sun, 06 Aug 1995 19:35
84 lines
2918 bytes
2918 bytes
In <3vqm8o$g7f@hrz-ws11.hrz.uni-kassel.de> Ralf Boecker
<boecker@hrz.uni-kassel.de> writes:
>
>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)
***
>
>
I also like this feature. But in C++ if we link some features with
someother one finally we are ending up with some ambiguities. OK. Here
what should be the behaviour for the following code ?
struct Base
{
Base () { ... }
Base (int) { ... }
}
struct Derv : Base
{
Derv () : Base () { ... }
Derv (int nVal) : Derv (), Base (nVal) { ... }
}
My question:
What should happen when the Derv class ctor with int argument is
getting called ?
- Ramesh.S
rameshs@ix.netcom.com
Technical Details
Message-ID:
<4035hm$g7s@ixnews7.ix.netcom.com>Path:
arc1-ca.pugleaf.net!ix.netcom.com!netnewsReferences:
<3v5lo9$pql@usenet.rpi.edu> <3vqm8o$g7f@hrz-ws11.hrz.uni-kassel.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!ix.netcom.com!netnews From: rameshs@ix.netcom.com (Ramesh Seshadri ) Newsgroups: comp.std.c++ Subject: Re: Calling ctors. from ctors. Date: 6 Aug 1995 19:35:18 GMT Organization: Netcom Message-ID: <4035hm$g7s@ixnews7.ix.netcom.com> References: <3v5lo9$pql@usenet.rpi.edu> <3vqm8o$g7f@hrz-ws11.hrz.uni-kassel.de> NNTP-Posting-Host: ix-pon-mi1-03.ix.netcom.com