Bug 4609 – clear() does not call base constructor if a class does not implement a default constructor
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-08-09T16:58:00Z
Last change time
2012-08-09T02:22:39Z
Assigned to
sean
Creator
lutger.blijdestijn
Comments
Comment #0 by lutger.blijdestijn — 2010-08-09T16:58:36Z
If a class only defines a constructor with one or more parameters and no default constructor, clear() does not call any constructor of the base classes nor does it throw an exception.
class A
{
int n = -1;
this() {
n = 0;
}
}
class B : A
{
// uncomment the following line and the unittest will pass
// this(){}
this(int b) {
n = b;
}
}
unittest
{
auto foo = new B(42);
assert( foo.n == 42 );
clear(foo);
assert( foo.n == 0 ); //fails
}
Comment #1 by verylonglogin.reg — 2012-08-09T02:22:39Z
`destroy` (new name of `clear`) shouldn't call constructors at all. It just destroys current object without creatuing a new valid one. Uncommenting constructor in derived class doesn't (and shouldn't) change anything. If it was, that was a bug (disappeared now) in `clear`.