A: 

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 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>;
}
Craig
@Craig: Thanks for sharing. I actually need the destructor to do some cleanup though. That's why. If I make it as protected, I get the same error saying cannot access protected member declared in...
jasonline
+2  A: 

The code you posted looks doesn't have any problem, so the problem must be in some other part of your source code.

The error message will be preceded by the filename and line number where the problem is occurring. Please look at the line and you'll see a bit of code that is either trying to call delete on a singleton pointer or is trying to construct an instance of singleton.

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

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

So in that case, you would want to see what is happening at line 41 in file.cpp.

R Samuel Klatchko
It's actually pointing at the header file, at the end of the file (the closing brace). And those are the only source codes in my workspace right now.
jasonline
Does this happen when compiling singleton.cpp or some other source file? What is the code right before `#include "singleton.h"`?
R Samuel Klatchko
@R Samuel Klatchko: Compiling singleton.cpp. Just another include file for stdafx.h. Someone was already able to reproduce it in one of the answers posted.
jasonline
A: 
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;
          }

     }
};
Ashish
@Mac: Thanks for sharing. But with this one, you actually need the caller to call RemoveInstance() right for the cleanup. I was thinking of an implementation where you don't need to ask the caller to call a cleanup function.
jasonline
This is the standard anti-pattern for writing a singleton. Please learn the correct (or should I say better ways to do it). Scott Myers has some really good examples.
Martin York
+1  A: 

You should probably have let us know that the version of Visual C++ you're working with is VC6. I can repro the error with that.

At this point, I have no suggestion other than to move up to a newer version of MSVC if possible (VC 2008 is available at no cost in the Express edition).

Just a couple other data points - VC2003 and later have no problem with the Singleton destructor being private as in your sample.

Michael Burr
@Michael: Sorry for missing that info. Thank you for your information.
jasonline
VC6 is pretty dang old nowadays - it makes sense to use it if you're maintaining code written for it, but for new projects please use something more modern. You'll save yourself a lot of unnecessary headaches (there's enough headache inducing stuff in C++ that you shouldn't pile it on with VC6 if you don't have to).
Michael Burr
@Michael: Yes, I'm maintaining old code so I thought of trying to compile just in this environment. Unfortunately, it's not working. I'll just have to try it on other compilers I guess.
jasonline
Or maybe just live with the dtor being public if you have to use VC6.
Michael Burr
+1  A: 

I'm no C++ or VC expert, but your example looks similar to the one described on this page ... which the author calls a compiler bug.

Stephen C
@Stephen: Thanks for the info. Your link provides a list of search results. I browsed over and I think it's this one - http://www.codeguru.com/forum/archive/index.php/t-236067.html.
jasonline
+1 if it was helpful give an upvote
stacker