static initialization question

Jarrett Billingsley jarrett.billingsley at gmail.com
Wed Dec 10 14:39:23 PST 2008


On Wed, Dec 10, 2008 at 5:31 PM, Weed <resume755 at mail.ru> wrote:
> code:
>
> import std.stdio;
>
> class MyClass
> {
>    invariant uint a = 0;
> }
>
> void main()
> {
>    static MyClass c = new MyClass;
>    writeln( c.a );
> }

It's not the class member that wants static initialization, it's your
variable declaration.

static MyClass c = new MyClass;

This is illegal because static variables must be initialized with
compile-time constants.  The simple way around this is:

static MyClass c; // defaults to null
c = new MyClass;

Which separates the declaration from initialization.


More information about the Digitalmars-d-learn mailing list