Interview with InformIT part 2/3

Vladimir Panteleev vladimir at thecybershadow.net
Wed Aug 18 04:46:56 PDT 2010


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.

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?

-- 
Best regards,
  Vladimir                            mailto:vladimir at thecybershadow.net


More information about the Digitalmars-d-announce mailing list