Making inheritance less tedious

Bill Baxter dnewsgroup at billbaxter.com
Mon Feb 26 22:54:06 PST 2007


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



More information about the Digitalmars-d mailing list