Making inheritance less tedious

Kristian Kilpi kjkilpi at gmail.com
Tue Feb 27 05:32:52 PST 2007


On Tue, 27 Feb 2007 08:54:06 +0200, Bill Baxter  
<dnewsgroup at billbaxter.com> wrote:
> Kevin Bealer wrote:
>> Kristian Kilpi wrote:
>>>
>>> I think class inheritance is a bit(?) tedious sometimes (that is,  
>>> unnecessarily tedious).
>>> (I leave interfaces out of this discussion.) 'Problems', IMHO, with it  
>>> are:
>>>
>>> 1) You cannot inherit constructors.
>>>
>>  As for (1), I agree and this is something that annoys me in C++ too.
>
> So what would be the effect of calling a base class constructor?
> I guess it would have to call the base constructor followed by the  
> derived class's default constructor?
>
> It seems like that could cause headaches.  Maybe that's why C++ doesn't  
> allow it.  Especially in D where the default constructor may already  
> call the base constructor.
>
> class Base
> {
>     this(int a, float b) {
>       m_a = a;
>       m_b = b;
>     }
>     int m_a;
>     float m_b;
> }
> class Derived : Base
> {
>     this() {
>        super(2,3.0f);
>        mString = "Happy happy";
>     }
>     this(int a) {
>        super(a,6.0f);
>        mString = "howdy howdy";
>     }
>     char[] mString;
> }
>
> void main()
> {
>     Derived x = new Derived(a,b);
>     // what got called exactly?
>     // and what values do x.m_a and x.m_b have now?
> }
>
> --bb

In my proposal I intented that you could inherit base class constructor  
only if you didn't define constructors in the derived class. So your  
example wouldn't compile because 'Derived' don't have 'this(int, float)'.

However, if a selective inheritance would be possible, then you could pull  
needed constructors from the base class to the derived class. But, as you  
mentioned, that can be problematic. I usually need to reimplement all the  
constructors or to inherit them from the base class as such. So, for me  
'all or nothing' approach would be sufficient.



More information about the Digitalmars-d mailing list