static initialization question

Weed resume755 at mail.ru
Wed Dec 10 15:20:27 PST 2008


Jarrett Billingsley пишет:
> 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.


In C++ analogous construction means creation of uninitialized static 
pointer (in compile time) and it's initialization at first execution of 
this line in the function.

Why D does not behave that way on this construction?


More information about the Digitalmars-d-learn mailing list