Possible Duplicate:
Cannot access private member in singleton class destructor
I'm implementing a singleton as below.
class A
{
public:
static A& instance();
private:
A(void)
{
cout << "In the constructor" << endl;
}
~A(void)
{
cout << "In the destructor" << endl;
}
};
A& A::instance()
{
static A theMainInstance;
return theMainInstance;
}
int main()
{
A& a = A::instance();
return 0;
}
The destructor is private. Will this get called for the object theMainInstance when the program is about to terminate?
I tried this in Visual studio 6, it gave a compilation error.
"cannot access private member declared in class..."
In visual studio 2010, this got compiled and the destructor was called.
What should be the expectation here according to the standard?
Edit : The confusion arises since Visual Studio 6 behaviour is not so dumb. It can be argued that the constructor of A for the static object is called in the context of a function of A. But the destructor is not called in the context of the same function. This is called from a global context.
But for this situation during the compilation causes an error
http://liveworkspace.org/code/a13eb44e21c01a2b32bd92382722350b – Ilya Lavrenov Jul 17 '12 at 14:05