How to write a singleton template?
Li Jie
cpunion at gmail.com
Tue Mar 14 17:55:58 PST 2006
I try to write a singleton template, but has not succeeded:
A:
template Singleton(T)
{
private T _instance;
static this()
{
_instance = new T();
}
public T instance ()
{
return _instance;
}
}
B:
template Singleton(T)
{
class Singleton
{
private static T _instance;
static this()
{
_instance = new T();
}
public static T instance ()
{
return _instance;
}
}
}
C:
template Singleton(T)
{
class Singleton
{
private static T _instance = null;
public static T instance ()
{
if (_instance == null)
_instance = new T();
return _instance;
}
}
}
// use it:
class AAA
{
public void hello ()
{
printf("hello\n");
}
}
int main(char[][] args)
{
alias Singleton!(AAA) aaa;
aaa.instance().hello(); // <== Segment fault !!!!!
return 0;
}
I want to know, how to write a singleton template?
Thanks.
- Li Jie
More information about the Digitalmars-d
mailing list