What is the best way to deal with this?

Martin martinbbjerregaard at gmail.com
Sat Feb 23 19:33:29 PST 2013


	import std.stdio;

	class TestClass(T)
	{
	private:
		__gshared TestClass[] globalInstances;
	public:
		this()
		{
			globalInstances ~= this;
		}
		
		void test()
		{
			writeln("Address of variable globalInstances is: 0x", 
globalInstances.ptr);
		}
		
	}

	void main(string[] args)
	{
	
		TestClass!(int) t1 = new TestClass!(int);
		TestClass!(string) t2 = new TestClass!(string);
		
		t1.test;
		t2.test;
		
		readln;

	}

Outputs:
Address of variable globalInstances is: 0x4F3F80
Address of variable globalInstances is: 0x4F3F60

Which I guess makes sense since there's seperate globalInstances 
variables generated per template instance of the class. I want to 
store ALL instances, no matter if it's a TestClass!(int) or 
TestClass!(string) though.

Should I just use a __gshared void*[] globalInstances outside of 
the template and cast when necessary or is there an easier way 
that I'm too stupid to see? It's really late here...


More information about the Digitalmars-d mailing list