[Issue 9066] Add constructor inheritance feature

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 4 12:01:30 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9066



--- Comment #8 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-10-04 12:01:23 PDT ---
(In reply to comment #6)
> (In reply to comment #5)
> 
> Inheriting the default constructor is implicit so why would you need an
> explicit syntax for non-default constructors?

I guess the real issue is that you could easily end up instantiating an object
which hasn't been properly initialized (properly meaning how the class designer
defines it). For example:

-----
class A
{
    this(float x) { this.x = x; }
    float x;
}

class B : A
{
    this(float x, float y) { super(x); this.y = y; }
    float y;
}

void main()
{
    auto b = new B(1.0);  // B's ctor not called
}
-----

Nevermind that we have .init, a class designer probably wants to have explicit
control of which constructors can be called by the user. If all base class
ctors are inherited.. well you saw the example.

Another interesting example:

-----
class A
{
    this(float x) { this.x = x; }
    float x;
}

class B : A
{
    this(float x, float y) { super(x); this.y = y; }
    const(float) y;
}

void main()
{
    // this should not compile since y must be initialized
    // in the class B ctor, which wasn't called.
    auto b = new B(1.0);
}
-----

So the user could easily get a half-constructed object, or bad diagnostics
about some const field not being initialized.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list