Static members and access attribute

Lars Kyllingstad public at kyllingen.NOSPAMnet
Wed Oct 22 05:31:31 PDT 2008


Aarti_pl wrote:
> Lars Kyllingstad pisze:
>> Uriel wrote:
>>> I've found some strange behaviour with static members and haven't 
>>> noticed anything about it in the documentation.
>>>
>>> module a;
>>>
>>> class A
>>> {
>>>     private static int Foo;
>>> }
>>>
>>> import std.stdio;
>>>
>>> import a;
>>>
>>> void main()
>>> {
>>>     A.Foo = 1;
>>>     writeln(A.Foo);
>>>
>>> /*        A obj = new A();
>>>     obj.Foo = 2;
>>>     writeln(A.Foo);
>>> */
>>> }
>>>
>>> Program will compile and print '1'. But if uncomment the second part 
>>> of code compiler will say that "class a.A member Foo is not accessible".
>>> Is this a bug?
>>
>> No, that's the way it should be. You get the error because of the line
>>
>>   obj.Foo = 2;
>>
>> Foo is not available for instances of A. There is only one (global) 
>> Foo, namely A.Foo. That's what static means.
>>
>> -Lars
> 
> I am not sure about what you say... In Java you can access static 
> members through objects - you get just warnings. As I said I am not sure 
> how it is supposed to be in D.
> 
> According to accessing private static member from another module - it is 
> bug. Its already in bugzilla.

OK, I didn't know that. Uriel, don't listen to me. :)

-Lars



More information about the Digitalmars-d mailing list