C++ Singleton class returning const reference
<p>I have an a class which is singleton as defined follows</p>

<pre><code>class myData {
private:
    myData (void); // singleton class.
    // Copy and assignment is prohibted.
    myData (const myData &amp;);
    myData &amp; operator=(const myData &amp;);
    static myData* s_pInstance;

public:
    ~myData (void);
    static const myData &amp; Instance();
        static void Terminate();

    void myFunc() { cout &lt;&lt; "my function..." ;}
};
</code></pre>

<p>// In cpp file.</p>

<pre><code>myData* myData::s_pInstance(NULL);

myData::myData(){}

myData::~myData()
{
    s_pInstance = NULL;
}

const myData&amp; myData::Instance()
{
    if (s_pInstance == NULL)
    {
        s_pInstance = new myData();
    }

    return *(s_pInstance); // want to avoid pointer as user may deallocate it, so i used const referense
}

void main() {

    (myData::Instance()).myFunc();

}
</code></pre>

<p>I am getting following error</p>

<p>error C2662: 'myData::myFunc' : cannot convert 'this' pointer from 'const myData' to 'myData&amp;'</p>

<p>how to avoid above problem and call a function from Instance function which is returning const reference?</p>

<p>Thanks!</p>
