Cannot access private member in singleton class destructor

前端 未结 5 963
感动是毒
感动是毒 2020-12-21 05:24

I\'m trying to implement this singleton class. But I encountered this error:

\'Singleton::~Singleton\': cannot access private member declared in class \'Singleton\'

5条回答
  •  渐次进展
    2020-12-21 05:55

    Personally i haven't put destructors in my singletons unless i am using a template singleton class, but then i make them protected.

    template
    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
    {
    friend class Singleton;
    }
    

提交回复
热议问题