I am trying to implement this singleton class. But I ran into this error:
'Singleton :: ~ Singleton': cannot access the private member declared in the class 'Singleton' This is marked in the header file, the last line contains the final figure.
Can someone help me explain what causes this problem? Below is my source code.
Singleton.h:
class Singleton { public: static Singleton* Instance() { if( !pInstance ) { if( destroyed ) {
Singleton.cpp:
Singleton* Singleton::pInstance = 0; bool Singleton::destroyed = false;
Inside my main function:
Singleton* s = Singleton::Instance();
If I make the destructor public, the problem goes away. But the book (Modern C ++ Design) says that it should be confidential so that users cannot delete the instance. I really need to put some cleanup code for pInstance and destroy inside the destructor.
By the way, I am using Visual C ++ 6.0 for compilation.
source share