composition vs inheritance

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Mar 16 08:40:41 PDT 2011


On 03/16/2011 07:01 AM, spir wrote:
> composition vs inheritance
>
> I have carefully read the document "Prefer composition to inheritance",
> from C++ coding standards, at
> http://www.artima.com/cppsource/codestandards3.html, by Herb Sutter and
> Andrei Alexandrescu.
>
> In general, I do agree in that composition makes for a simpler scheme,
> and more flexible. But when writing D code, I constantly step on the
> same issue that without inheritance and (runtime type) polymorphism, I
> simply cannot express /very/ common things.

You are exaggerating the frequency of certain situations and needs.

> For instance, say I have 3 kinds of Xs X1 X2 X3.

That's not such a common encounter. There are many conditions that the 
three kinds must satisfy - such as they must have a lot in common and 
very few differences. Also, all differences must have been thought over 
at X's creation time and put in the interface of X. THEN you are 
supposed to use an inheritance hierarchy.

This is less frequent than people initially thought. In the 1980s it was 
believed that everything would belong in a hierarchy and the only 
challenge was to find them "good" hierarchies. Now it is quite clear 
things aren't all that simple.


Andrei


More information about the Digitalmars-d mailing list