Static member inside a class.

w0rp devw0rp at gmail.com
Thu Jun 13 13:00:58 PDT 2013


On Thursday, 13 June 2013 at 19:47:23 UTC, Agustin wrote:
> I would like to know if static members are shared between 2 
> library. For example:
>
> Class A
> {
>  static uint var;
> }
>
> From Library A:
>
> A::var = 3;
>
> From Library B:
>
> if( A::var == 3 )
>   ...
>
> Its this possible? if not, its any way to make it happend?

The members are shared between different modules, yes. You use . 
instead of :: for scope resolution. You can also initialise a few 
things in a static constructors at class scope...

class A {
     static uint var;

     static this() {
         var = 3;
     }
}

Or at module scope...

class A {
     static uint var;
}

static this() {
     A.var = 3;
}

You should note that the data isn't shared between threads by 
default, but that's another detail. You can also usually produce 
a design that uses non-static data instead of static data.


More information about the Digitalmars-d-learn mailing list