Compile-time reference type objects construction (Was - Re: struct

Denis Koroskin 2korden at gmail.com
Mon Dec 22 08:02:29 PST 2008


Kagamin Wrote:

> Denis Koroskin Wrote:
> 
> > class Widget
> > {
> >     WidgetFactory* factory = &defaultFactory;
> > }
> > 
> > void main()
> > {
> >     Widget w = new Widget();
> >     writefln(w.factory.someParameterValue); // prints 14
> > }
> 
> You initialize member field here. It's usually done in instance constructor.
> 
> class Widget
> {
>     WidgetFactory factory;
>     this(){ factory = defaultFactory; }
> }

I don't agree with you. If so, then why we have the following syntax allowed:

class Foo
{
    int i = 42;
}

if we could just use

class Foo
{
    this()
    {
        i = 42;
    }
}

?

Imagine you have lots of ctors (with different arguments), should you put i = 42; into every one? Or move it into some initialize() method? Don't you think it is too verbose?


More information about the Digitalmars-d-learn mailing list