One more thing:<br><br>Is it possible for the compiler to detect missing constructors in a class?<br><br>E.g.:<br><br>class A<br>{<br> static A a1;<br> static A a2;<br> <br> static ~this()<br> {<br> clear(a1);<br>
}<br><br> static ~this()<br> {<br> clear(a2);<br> }<br> <br>}<br><br>void main() { }<br><br><br>This will cause an object access violation in runtime.<br><br><div class="gmail_quote">On Sun, Aug 8, 2010 at 8:09 PM, Andrej Mitrovic <span dir="ltr"><<a href="mailto:andrej.mitrovich@gmail.com">andrej.mitrovich@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">On Page 187 it states (shortened here for convenience):<br>
"By calling clear(b), you invoke b's destructor, obliterate the object's state with Buffer.init, and call Buffer's default constructor"<br>
<br>
On the next page there's a unittest, and the entire example is this:<br>
<br>
import core.stdc.stdlib;<br>
<br>
class Buffer<br>
{<br>
private void* data;<br>
<br>
// constructor<br>
this()<br>
{<br>
data = malloc(1024);<br>
}<br>
<br>
// destructor<br>
~this()<br>
{<br>
free(data);<br>
}<br>
}<br>
<br>
unittest<br>
{<br>
auto b = new Buffer;<br>
auto b1 = b; // extra alias for b<br>
clear(b);<br>
assert(b1.data is null);<br>
}<br>
<br>
void main() { }<br>
<br>
The assert fails, regardless if there is another reference to the object or not (b1 in this case).<br>
<br>
I've added some writeln()'s (not shown here), and just like the book said, after clear(b), b's destructor gets called, the fields get initialized to .init, and then b's constructor gets called. But the constructor will allocate memory for b1.data again, which means data is not null anymore.<br>
<br>
So I'm guessing the assert code is wrong in the example?<br>
</blockquote></div><br>