Programming in D page 295 - disable default constructor
Brother Bill
brotherbill at mail.com
Tue Feb 10 13:23:43 UTC 2026
On Tuesday, 10 February 2026 at 12:50:16 UTC, monkyyy wrote:
> On Tuesday, 10 February 2026 at 12:04:05 UTC, Brother Bill
> wrote:
>> When adding an explicit constructor such as ```this(string
>> fileName)```, this is "supposed" to remove the "default"
>> constructor. Then the developer needs to add back the default
>> constructor.
>>
>> [...]
>
> ```d
> import std;
> struct foo{
> int i,j;
> this(int a, int b){
> j=a;
> i=b;
> }
> }
> unittest{
> auto bar=foo(1,2);
> bar=foo(3);
> }
> ```
>
>> Error: constructor `onlineapp.foo.this(int a, int b)` is not
>> callable using argument types `(int)`
>
> working as expected
Perfectly true as foo(3) has no explicit constructor.
If you remove this(int a, int b), it will also fail to compile.
My question is about the *parameterless* constructor:
``` auto a4 = Archive();```
I am unable to define a parameterless constructor for it.
Is the only alternative to prevent a parameterless construction
to use ```@disable this();``` ?
More information about the Digitalmars-d-learn
mailing list