Unable to access private member in singleton class destructor

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 ) { // throw exception } else { Create(); } } return pInstance; } private: static void Create() { static Singleton myInstance; pInstance = &myInstance; } Singleton() {} Singleton( const Singleton& ); Singleton& operator=( const Singleton& ); ~Singleton() { pInstance = 0; detroyed = false; } static Singleton* pInstance; static bool 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.

+4
source share
5 answers

You should probably tell us that the version of Visual C ++ you are working with is VC6. I can reproduce the error with this.

At the moment, I have no proposal, except to upgrade to a newer version of MSVC, if possible (VC 2008 is available for free in the Express version).

Only a couple of other data points - VC2003 and later have no problems with the Singleton destructor being private, as in your example.

+2
source

You had no problems with the code you posted, so the problem should be in some other part of your source code.

The error message will be followed by the file name and line number in which the problem occurs. Look at the line and you will see some code that either tries to call delete on a single-point pointer, or tries to create a singleton instance.

The error message will look something like this (file and line number is just an example):

 c:\path\to\file.cpp(41) : error C2248: 'Singleton::~Singleton': cannot access private member declared in class 'Singleton' 

So, in this case, you need to see what happens on line 41 in file.cpp.

+2
source

I am not an expert on C ++ or VC, but your example is similar to the one described on this page ... which the author causes a compiler error.

+2
source

Personally, I did not put destructors in my singleton unless I use the singleton class template, but then I protect them.

 template<class T> class Singleton { public: static T &GetInstance( void ) { static T obj; return obj; } static T *GetInstancePtr( void ) { return &(GetInstance()); } protected: virtual ~Singleton(){}; Singleton(){}; }; 

then write my class as

 class LogWriter : public Singleton<LogWriter> { friend class Singleton<LogWriter>; } 
0
source
 class Singleton { static Singleton *pInstance = NULL; Singleton(){}; public: static Singleton * GetInstance() { if(!pInstance) { pInstance = new Singleton(); } return pInstance; } static void RemoveInstance() { if(pInstance) { delete pInstance; pInstance = NULL; } } }; 
-1
source

Source: https://habr.com/ru/post/920690/


All Articles