In Programming in D book, page 324, I've expanded this code as follows:

source/app.d
```
void main()
{
        auto variable1 = new MyClass(1);
        auto variable2 = new MyClass(2);

// variable2 disassociates from new MyClass(2), where GC (garbage collector) can reclaim its memory // now both variable1 and variable2 are both referencing new MyClass(2)
        variable1 = variable2;

        assert(variable1 is variable2);
}

class MyClass
{
        int id;

        // this(int id)
        // {
        //      this.id = id;
        // }
}

```

This fails to compile with message:
```
c:\temp\c54_p324_1d_assignment\source\app.d(5): Error: no constructor for `MyClass`
    auto variable1 = new MyClass(1);
                     ^
c:\temp\c54_p324_1d_assignment\source\app.d(6): Error: no constructor for `MyClass`
    auto variable2 = new MyClass(2);
```

Is this expected behavior, that is, we always need to create our constructors, no free constructor is built for us by the D compiler?

If so, this is a clear departure from C# and Eiffel, where one gets a free constructor if no explicitly declared constructors.
  • Do classes requi... Brother Bill via Digitalmars-d-learn
    • Re: Do clas... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
      • Re: Do ... Brother Bill via Digitalmars-d-learn
        • Re:... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
          • ... Mike Parker via Digitalmars-d-learn
            • ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
              • ... Brother Bill via Digitalmars-d-learn
                • ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
            • ... Mindy (0xEAB) via Digitalmars-d-learn
              • ... Mindy (0xEAB) via Digitalmars-d-learn
        • Re:... Andy Valencia via Digitalmars-d-learn

Reply via email to