Interview with InformIT part 2/3
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Wed Aug 18 08:13:29 PDT 2010
On 08/18/2010 06:46 AM, Vladimir Panteleev wrote:
> On Wed, 18 Aug 2010 13:13:25 +0300, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>
>> http://www.informit.com/articles/article.aspx?p=1622265
>
> Thanks, that was an interesting read.
>
> It's possible that I'm missing something, but I think that C++'s default
> constructors + reference-type structs/classes allow a pattern which
> isn't easily translatable to D. For example, in C++...
>
> class A { /* a class with a default constructor */ };
>
> struct B { A a; /* ... */ };
>
> struct C { B b; /* ... */ };
>
> Now, instantiating C will invoke A's constructors. In order for this to
> work, the compiler automatically generates hidden constructors for B and
> C. However, D doesn't have default constructors for structs (and,
> according to TDPL, never will)? D does seem to generate hidden postblit
> constructors and destructors, though.
Don't forget that non-default constructors are fair game though. I agree
that for this idiom it would be nice to have the default constructor
guarantee execution of certain initializations.
> If I had to port a C++ project to D which made heavy use of this
> pattern, what would be the best way to do it? The only ways I see is
> either rewriting the code to use classes (which means writing
> constructors with explicit instantiation, more dereferences and heap
> usage leading to worse performance...), or implementing and calling
> pseudo-constructors in both B and C, and (most importantly) all places
> which "instantiate" C. Did I miss anything?
One simple technique I use is to write and use factory functions. They
are particularly handy when C is also a template struct.
Andrei
More information about the Digitalmars-d-announce
mailing list