What is the best way to deal with this?

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Feb 23 19:54:20 PST 2013


On 2/24/13, Martin <martinbbjerregaard at gmail.com> wrote:
> Should I just use a __gshared void*[] globalInstances outside of
> the template and cast when necessary..?

Technically they all inherit Object so you can use:

__gshared Object[] globalInstances;

As for where to put them:

template TestClassWrap()
{
    __gshared Object[] globalInstances;

    class TestClassWrap(T)
    {
    public:
        this()
        {
            globalInstances ~= this;
        }

        void test()
        {
            writeln("Address of variable globalInstances is: 0x",
                    globalInstances.ptr);
        }
    }
}

alias TestClassWrap!() TestClass;

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

    t1.test;
    t2.test;
}


More information about the Digitalmars-d mailing list