Singleton Pattern with struct

ParticlePeter ParticlePeter at gmx.de
Thu Jan 24 06:11:09 PST 2013


Hi,

I am trying to figure out the singleton pattern with a struct 
instead of a class:
[code]
struct Singleton  {

private :
	this( int a = 0 ) {} ;
	static Singleton * s ;

public :
	@disable this() ;
	static ref Singleton instance()  {
		if ( s is null )
			s = new Singleton( 0 ) ;
		return * s ;
	}
	
	int val = 0 ;
}
[/code]

This compiles, but when I use it:
[code]
	auto s = Singleton.instance ;
	writeln( s.val ) ;

	Singleton.instance.val = 2 ;
	writeln( s.val ) ;
[/code]

I get:
0
0

Where is my mistake ?

Cheers, PP !


More information about the Digitalmars-d-learn mailing list