Inheriting constructors

Bill Baxter dnewsgroup at billbaxter.com
Sat Jun 2 04:35:27 PDT 2007


Jari-Matti Mäkelä wrote:
> Chris Nicholson-Sauls wrote:
> 
>> Jari-Matti Mäkelä wrote:
>>> Sean Kelly wrote:
>>>
>>> A bit off-topic (sorry), but you're using protection modifiers here:
>>>
>>>>      class PrintOnDestruct( T ) :
>>>>          public T
>>> and here
>>>
>>>>      class Placed( T ) :
>>>>          public T
>>> Are you also suggesting that D should support those like C++ does?
>> Actually, they are already allowed.  Check the grammar at the online docs:
>> http://digitalmars.com/d/class.html
>>
>> You will see that it allows both 'SuperClass' and 'InterfaceClass' to be
>> preceded by a
>> 'Protection'.  That said, I don't know that I've ever seen anyone actually
>> bother using this in D, nor am I sure of its utility.
> 
> They have been there and I have asked this previously, yes. However, the
> compiler does not use them (yet?).
> 
> I think they cause problems:
> 
>   // module 1
>   interface foo { void method(); }
>   class base : foo { void method() {} }
>   class moo: private base {}
> 
>   // module 2
>   void main() {
>     foo m = new moo();
>     m.method(); // bang, runtime error
>   }
> 
> Maybe this is something C++ programmers like, but in java/c# world there are
> others ways to do this. And I think the inheritance model in D is closer to
> Java than C++ so that's why it feels weird to me.

Well, that's sort of the whole point of private base classes.  You're 
using the base as an implementation detail.  To the outside world it 
should look more or less like you DON'T derive from base.
But such a thing definitely doesn't make as much sense when you only 
have single inheritance.  A private mixin would probably make more sense 
in D.  Of course if you're trying to re-use something that wasn't a 
template mixin to begin with, then you're out of luck.  Which is why the 
C++ is handy if you have multiple inheritance.  It basically lets you 
mixin a pre-exising class privately.

--bb



More information about the Digitalmars-d mailing list