Why isn't field-wise constructor automatic for structs and not classes?
John Colvin via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jan 2 04:18:16 PST 2016
On Saturday, 2 January 2016 at 02:12:19 UTC, Shriramana Sharma
wrote:
> If I have:
>
> struct TimeSpan { double start, end; }
>
> Then both the following automatically work:
>
> auto s = TimeSpan();
> auto t = TimeSpan(1, 2);
>
> But if I make it a class (I need to) then I have to explicitly
> define a field-wise constructor else only a constructor with no
> args is automatically defined. Why can't the field-wise
> functionality be automatic for classes too?
Strictly speaking you aren't calling a constructor there, you're
writing a struct literal.
More information about the Digitalmars-d-learn
mailing list