Thread View: comp.std.c++
2 messages
2 total messages
Started by Lance Page
Fri, 04 Aug 1995 15:05
Why not A::A(something) : A(other) {} ?
Author: Lance Page
Date: Fri, 04 Aug 1995 15:05
Date: Fri, 04 Aug 1995 15:05
69 lines
3203 bytes
3203 bytes
I propose that a constructor for a class A be allowed to invoke another
constructor of A in place of its base class & member initializers.
e.g.:
class A {
A(other);
A(something) : A(other) {}
...
};
Motivation:
-----------
** Source code advantages:
If there are key requirements for constructors of this class,
(e.g. initializing some special pointers or references), it is tedious,
error-prone and bulky to have to replicate these initializations in all
the constructors.
** Object code size: Assume the definition A::A(something) : A(other) {}
If A::A(other) was of significant size and not inlined, then
A::A(something), whether inline or not, could reuse this object code.
** Speed: Compare this to the technique by which multiple ctors share an init(..) method...
-- The init() version calls separate construct(empty) and
initialize(as desired) methods for each *member* object, even if a single
construct(as desired) should suffice. These unnecessary costs add up...
-- If the init() method can be called at any time in an object's life,
then it may have to check the prior state of the object and "clean it up",
before initializing the object to a new value. Thus, the member objects get
constructed(empty), tested, and then init'd(as desired), instead of
constructed(as desired).
Any one of these considerations can currently be worked around by careful
design, but the status quo forces an unnecessary trade-off between these
source code, object code size, and speed considerations.
Limitations:
-----------
** It makes sense to dictate that using this feature precludes
using any other base class/ member initializers (i.e. before the '{')
in this constructor.
Reasons:
(1) because the ctor being called by this ctor implicitly acts upon the
entire object.
(2) such a restriction conveniently avoids complicated "what about all
these combinations of initializers..." issues-- making this a feature
that non-lawyers can understand.
Implementation:
---------------
** Seems obvious (to someone who has never implemented a compiler...)
** Normal declaration-order and inlining rules apply.
Prior discussion:
-----------------
Extracted from Marshall P. Cline's C++ FAQ:
> Q18: How can I make a constructor call another constructor as a primitive?
> No way.
> [explanation that calling the ctor ends up ct'ing a secondary local object...]
...But there's no explanation why a mechanism to do "what is desired" is
provided.
(And using the method I suggest, it is clear that you want to call
the other ctor on "*this", not some secondary local object.)
I'd be surprised if a proposal like this one hadn't been discussed
before--- it's simple, and the benefits are real.
Anyway... "why not?".
-Lance Page
Re: Why not A::A(something) : A(other) {} ?
Author: David Vandevoorde
Date: Fri, 04 Aug 1995 23:03
Date: Fri, 04 Aug 1995 23:03
33 lines
1332 bytes
1332 bytes
In article <3vtd09$83f@usenet.rpi.edu>, Lance Page <page@cat.rpi.edu> wrote:
>
>I propose that a constructor for a class A be allowed to invoke another
>constructor of A in place of its base class & member initializers.
>e.g.:
>
>class A {
> A(other);
> A(something) : A(other) {}
> ...
>};
>
[...]
>Anyway... "why not?".
>
>-Lance Page
Except for the initializer-list, I don't see how factoring out the
common construction adds significant overhead:
struct A {
A("other"): "init_list" { ... construct(common); ... }
A("something"): "init_list" { ... construct("common"); ... }
// ...
private:
void construct("common") ...
// ...
};
The initializer list must be repeated, but I think that is rarely
a problem, is it?
Daveed
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads