How to init a static object ?
Kirk McDonald
kirklin.mcdonald at gmail.com
Sat Mar 15 12:57:00 PDT 2008
TSalm wrote:
> Hi all,
>
> I would do something like this below, but DMD return me the error :
> testStaticInit.d(10): Error: non-constant expression new A
> on the line "public static A a=new A();"
> Is there a simple way to instanciate a static class ?
>
>
>
> class A
> {
> public this() {}
> }
>
> class B
> {
> public static A a = new A();
>
> public this() {}
>
> }
>
> void main()
> {
> new B();
> }
>
>
>
> Thanks in advance,
> TSalm
>
>
(One incidental note: Your use of 'public' is unnecessary, as everything
is public by default in D.)
Use a static constructor:
class A {}
class B {
static A a;
static this() {
a = new A;
}
}
--
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org
More information about the Digitalmars-d-learn
mailing list