Prevent duplicate objects in classes/instance access with singleton : <p>The use is to provide .instance access, also to prevent duplicate objects of those classes.</p>

<p>Is this a good code implementation of a singleton?</p>

<pre><code>template &lt;typename T&gt; class singleton
{
public:
    static T *ms_singleton;

    singleton()
    {
        assert(!ms_singleton);
        long offset = (long)(T *) 1 - (long)(singleton &lt;T&gt; *)(T *) 1;
        ms_singleton = (T *)((long) this + offset);
    }

    virtual ~singleton()
    {
        assert(ms_singleton);
        ms_singleton = 0;
    }

    static T &amp;instance()
    {
        assert(ms_singleton);
        return (*ms_singleton);
    }

    static T &amp;Instance()
    {
        assert(ms_singleton);
        return (*ms_singleton);
    }

    static T *instance_ptr()
    {
        return (ms_singleton);
    }
};
template &lt;typename T&gt; T *singleton &lt;T&gt;::ms_singleton = NULL;
</code></pre>

<p>How I use it:</p>

<pre><code>class test1: public singleton&lt;test2&gt;
{
    // Something
};
</code></pre>

<p>If not, what is wrong here? What should i rewrite here?</p>
