[Issue 12737] static constructor requires call of super constructor
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Mon May 12 13:50:28 PDT 2014
https://issues.dlang.org/show_bug.cgi?id=12737
--- Comment #3 from Jonathan M Davis <jmdavisProg at gmx.com> ---
The static constructor has nothing to do with the error. Look at my
counter-example. It's exactly the same except for the fact that it has not
static constructor. The problem is that a constructor for B must call A's
constructor, e.g.
class B : A
{
this()
{
super(42);
}
}
but you didn't declare one, and the one created by the compiler looks like
class B : A
{
this()
{
super();
}
}
but A doesn't have a default constructor, so that doesn't work, and so the
compiler gives you the error that it's giving you. It's telling you that you
need to either give A a default-constructor so that B's compiler-generated
default constructor will work, or you need to declare a default constructor for
B which calls the constructor that A does have.
static constructors have nothing to do with inheritance and will work just fine
regardless of whether a class has static members (it just won't have much to do
if the class doesn't have any static members). So, if the static constructor in
B were complaining about the lack of one in A, then yes, that would be a bug in
the compiler. But that's not what's happening. The compiler is complaining
about the fact that it can't generate a valid default constructor for B due to
the lack of a default constructor in A.
This code would compile just fine
class A {
this(int a) {
}
}
class B : A {
static this() {
}
this()
{
super(42);
}
}
void main() {
auto n = new B();
}
as would this code
class A {
this(int a) {
}
this() {
}
}
class B : A {
static this() {
}
}
void main() {
auto n = new B();
}
--
More information about the Digitalmars-d-bugs
mailing list